LINQ 链接Where 子句

发布于 2024-11-04 06:30:41 字数 1024 浏览 0 评论 0原文

我正在尝试编写一个动态搜索 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2024-11-11 06:30:41

如果您想过滤相关记录,则使用 include 时是不可能的。包含仅允许加载所有属性。在单个查询中获取过滤导航属性的唯一可能方法是手动查询并投影到非实体或匿名类型:

var query = from p in context.Persons
            select new 
            {
                Person = p,
                Addresses = p.PersonAddreeses
                             .Where(pa => pa.Address.Country == "Ireland")
            };

如果您想获取包含爱尔兰地址的人员而不过滤加载的地址,则不能使用您定义的查询。其中期望表达式产生 bool 而不是另一个查询。试试这个:

query = query.Where(p => p.PersonAddresses
                          .Any(pa => pa.Address.Country == "Ireland");

我完全不确定您的示例中的 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:

var query = from p in context.Persons
            select new 
            {
                Person = p,
                Addresses = p.PersonAddreeses
                             .Where(pa => pa.Address.Country == "Ireland")
            };

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:

query = query.Where(p => p.PersonAddresses
                          .Any(pa => pa.Address.Country == "Ireland");

I'm absolutely not sure what is Search in your example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文