【发布时间】:2019-04-24 21:43:06
【问题描述】:
我有两个模型:TestRecord 和 Part,其中 TestRecord 包含对 Part 的引用:
TestRecord
----
public string TestRecordId
public int PartId <-- foreign key to parts table
public string Name
public string TestType
public virtual Part Part
Part
----
int PartId
string Name
string Description
public virtual ICollection<TestRecord> TestRecords
我有一个搜索页面,它显示测试记录的每个属性的输入,包括其相关属性,例如:
@model TestRecord
<!-- From TestRecord -->
<input asp-for="TestType" type="text" />
<!-- From TestRecord.Part assoc. prop -->
<input asp-for="Part.Name" type="text" />
....
And so on...
当我将其发布到我的控制器以运行查询时,处理此查询的最佳方式是什么?我有 20 多个属性,这些属性可能会或可能不会在有助于过滤查询以返回 List<TestRecord> 的页面上填写。
如果我只有几个要查询的属性,并且我知道它们肯定会被填充,我可以执行以下操作:
[HttpPost]
public List<TestRecord> Search(TestRecord testRecord){
List<TestRecord> records = _db.TestRecords
.Where(tr => tr.TestType == testRecord.TestType)
.Where(tr => tr.Part.Name == testRecord.Part.Name).ToList();
return records;
}
我将如何生成一个 LINQ 查询,如上所述,该查询从模型中查询所有非空/非空属性?将所有属性硬编码到我的查询中是我唯一的选择吗?
【问题讨论】:
标签: c# linq asp.net-core linq-to-sql