LINQ 链接Where 子句
我正在尝试编写一个动态搜索 I 和链接 where 子句,我有下面的类和示例代码。
public class Person
{
public int PersonId { get; set; }
//More Fields ..
}
//Link Table
public class PersonAddress
{
public int PersonID { get; set; }
public int AddressID { get; set; }
//More Fields ..
}
public class Address
{
public int AddressId { get; set; }
public int ReferenceDataID { get; set; }
//More Fields ..
}
public class ReferenceData
{
public int ReferenceDataId { get; set; }
public string MyData { get; set; }
//More Fields ..
}
var query = (from p in People.Include("PersonAddresses")
.Include("PersonAddresses.Address")
.Include("PersonAddresses.Address.ReferenceData")
select p);
if (!String.IsNullOrEmpty(searchName))
query = query.Where(q => q.Search.Contains(person.SearchName));
// How can I access the MyData
if (!String.IsNullOrEmpty(searchCountry))
query = query.Where(q => q.Search.Where(a => a. == "Ireland");
谢谢。
I am trying to write a dynamic search I and chain where clauses I have my classes below and sample code.
public class Person
{
public int PersonId { get; set; }
//More Fields ..
}
//Link Table
public class PersonAddress
{
public int PersonID { get; set; }
public int AddressID { get; set; }
//More Fields ..
}
public class Address
{
public int AddressId { get; set; }
public int ReferenceDataID { get; set; }
//More Fields ..
}
public class ReferenceData
{
public int ReferenceDataId { get; set; }
public string MyData { get; set; }
//More Fields ..
}
var query = (from p in People.Include("PersonAddresses")
.Include("PersonAddresses.Address")
.Include("PersonAddresses.Address.ReferenceData")
select p);
if (!String.IsNullOrEmpty(searchName))
query = query.Where(q => q.Search.Contains(person.SearchName));
// How can I access the MyData
if (!String.IsNullOrEmpty(searchCountry))
query = query.Where(q => q.Search.Where(a => a. == "Ireland");
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想过滤相关记录,则使用 include 时是不可能的。包含仅允许加载所有属性。在单个查询中获取过滤导航属性的唯一可能方法是手动查询并投影到非实体或匿名类型:
如果您想获取包含爱尔兰地址的人员而不过滤加载的地址,则不能使用您定义的查询。其中期望表达式产生
bool
而不是另一个查询。试试这个:我完全不确定您的示例中的
Search
是什么。If you want to filter related records it is not possible when using includes. Include allows only loading all properties. The only possible way to get filtered navigation properties in single query is manual query with projection to non entity or anonymous type:
If you want to get person which contains address from Ireland without filtering loaded addresses you cannot use query you defined. Where expects expression resulting in
bool
not another query. Try this:I'm absolutely not sure what is
Search
in your example.