【问题标题】:Entity Framework - Linq To Entities - Many-To-Many Query Problems实体框架 - Linq To 实体 - 多对多查询问题
【发布时间】:2009-04-27 14:06:41
【问题描述】:

我在 Linq To Entities 中查询多对多关系时遇到问题。 我基本上是在尝试使用 Linq 复制此查询:

Select * 
FROM Customer 
LEFT JOIN CustomerInterest ON Customer.CustomerID = CustomerInterest.CustomerID
LEFT JOIN Interest ON CustomerInterest.InterestID = Interest.InterestID
WHERE Interest.InterestName = 'Football'

我浏览了网络并没有真正找到任何合适的示例来说明如何做到这一点。我得到的最接近的是:

List<Customer> _Customers = (from _LCustomers in _CRM.Customer.Include("CustomerInterest.Interest")
                                  where _LCustomers.CustomerInterest.Any(x => x.Interest.InterestName == "Football")
                                  select _LCustomers).ToList();

这样做的问题是,如果客户有多个兴趣并且其中之一是“足球”,那么所有这些都会被退回。我还查看了具有逆问题的 All(),即只有当他们有一个兴趣并且是足球时才会返回,如果他们有两个并且其中一个不是足球,则不会返回任何内容。

有人有什么想法吗?

【问题讨论】:

标签: entity-framework linq-to-entities many-to-many left-join


【解决方案1】:

试试这个,

var result = from c in ctx.Customer
             from i in c.Interest
             where i.InterestName == "Football"
             select c;

希望这会有所帮助,

雷。

【讨论】:

    【解决方案2】:

    我不确定你想要获得什么。具有客户兴趣和兴趣的客户列表?只需根据客户的兴趣开始查询即可。

    context.CustomerInterest.
       Where(ci => ci.Interest.InterestName == "Football").
       Select(ci => new
       {
          Customer = ci.Customer,
          CustomerInterest = ci,
          Interest = ci.Interest
       });
    

    但这是非常多余的。为什么不直接获取匹配的客户兴趣?

    IEnumerable<CustomerInterest> customerInterests = context.CustomerInterest.
       Where(ci => ci.Interest.InterestName == "Football");
    

    您仍然可以访问其他信息,而无需明确存储。

    foreach (CustomerInterest customerInterest in customerInterests)
    {
       DoSomething(customerInterest);
       DoSomething(customerInterest.Customer);
       DoSomething(customerInterest.Interest);
    }
    

    【讨论】:

    • 这个想法是使用客户作为查询的基础返回所有相关数据的客户,并带有单个数据库命中。返回的数据将被序列化并在单个 wcf 调用中返回给客户端(用于客户维护屏幕)。由于我们试图使客户端保持苗条并尽可能通用,因此客户端没有 edmx 中实体的概念,它只适用于反序列化的 xml 并与之一起工作。我试图避免匿名类型并返回一个 Customer 对象,其中仅包含与 where 子句匹配的相关数据。
    【解决方案3】:
            var results = from c in _CRM.Customer
                          from ci in c.Interests
                          join i in _CRM.Interests
                          on ci.ID equals i.ID
                          where i.Interest = "Football"
                          select c;
    

    【讨论】:

      【解决方案4】:

      如果你想保持它的通用性,更好的方法是使用实​​体 sql[Esql]。 Coz L2E 不支持 linq 查询中的集合位置。

      你不能使用

      customer.Interests.Where(interest => interest.Name =='FootBall')

      查询看起来像这样..

      context.CreateQuery(@"SELECT VALUE Customer FROM Customer WHERE EXISTS( SELECT VALUE FROM CustomerInterest WHERE CustomerInterest.Ineterest.Name = 'FootBall')).Include("Interest");

      希望对你有帮助!

      【讨论】:

      • 难道不放弃到 esql 来寻求解决方案只是感觉像一个 hack 吗?你失去了所有的智能以及几乎所有应该成为使用 EF 的重点。那时还不如在 DbReader 中执行它。
      • 是的.. 但是与 DbReader 在 ADO 中返回的数据记录相比,您在 EF 中获得了一个类型化的实体客户。
      【解决方案5】:

      对于 LINQT 来说,这太多了。尝试在您的数据库中使用视图或按照 Deepak N 所说的那样工作。 最好的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多