3

I am using EF4.3. At the back end I have a database that includes the following tables

enter image description here

In my site logic, I retrieve the entity Tier, but also eager load MatchNode, MatchBuyer and Buyer.

Here's my logic for fetching all Tier entities:

    public static IEnumerable<Tier> Fetch()
    {
        using (var uow = new UnitOfWork(ConnectionString.Lenders))
        {
            var r = new Repository<Tier>(uow.Context);

            return r.Find()
                .Include("MatchNodes.MatchBuyer.Buyer")           
                .ToList();
        }
    }

As you can see, a Tier has many MatchNode, and each MatchNode, a single MatchBuyer and MatchBuyer a single Buyer.

However what I want to do is this. Only include MatchNode's where enabled = true and Buyer: Status = 'Active'.

Is this possible and can it be incorporated into the logic below as a .Where()?

4

1 回答 1

2

你可以试试这个:

 return r.Find()
         .Include("MatchNodes.MatchBuyer.Buyer")   
         .Where(c => c.MatchNodes
                         .Any(c => c.Enabled == true && 
                                   c.MatchBuyer.Buyer.Status == "Active")
                   )        
          .ToList();

它会给你所有的TiersMatchNodes.Enabled == true
Buyer.Status == "Active"

编辑
只返回MatchNotes使用 a SelectMany

var lst =   r.Find()
             .Include("MatchNodes.MatchBuyer.Buyer")   
             .Where(c => c.MatchNodes
                             .Any(c => c.Enabled == true && 
                                       c.MatchBuyer.Buyer.Status == "Active")
                       )
              .SelectMany(c => c.MatchNodes)  //<-- SelectMany istead of Select       
              .ToList();

编辑 2
好的 - 所以上面的代码将找到至少有一个匹配节点已启用且buyerstatus 为 actice 的项目 ( Tiers) 。 MatchNode

我们可以通过替换.Any(..).All(..).
这将返回所有满足需求Tiers地方。MatchNodes

于 2013-06-17T09:17:10.280 回答