如何在结果中添加满足哪些OR条件

时间:2011-01-13 21:07:22

标签: linq entity-framework entity-framework-4

在以下LINQ查询中,我将返回符合某些条件的人员。在标准中,我有OR条件。如何返回该人遇到的OR条件?我想在.Select语句中包含一个x.AttId。每个人可以同时分配许多AttIds。

var DNR = dc.Contacts.Where(x => x.Type == 1 &&
                         x.Att.Any(caa =>
                                   caa.ContactID == x.ContactID &&
                                   ( caa.AttID == 102 || caa.AttID == 103 )
                                   )
                           )
   .Select(x => new {x.ContactID, x.FirstName, x.LastName})
   .OrderBy (x => x.ContactID)

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

var DNR = dc.Contacts.Where(x => x.Type == 1 
       && x.Att.Any(caa => caa.ContactID == x.ContactID && 
           (caa.AttID == 102 || caa.AttID == 103)))
   .Select(p => new 
      {
         p.Att.First(r => r.ContactID == p.ContanctID 
           && (r.AttID == 102 || r.AttID == 103)).AttID,  
         p.ContactID, 
         p.FirstName, 
         p.LastName
      }
   ).OrderBy (q => q.ContactID)

答案 1 :(得分:0)

如果您想知道匹配的AttID的独特集合,可以选择此选项。

var DNR =
    from x in dc.Contacts
    from caa in x.Att
    where x.ContactID == caa.ContactID
    where caa.AttID == 102 || caa.AttID == 103
    group caa.AttID
        by new { x.ContactID, x.FirstName, x.LastName, }
        into gs
    orderby gs.Key.ContactID
    select new
    {
        Contact = gs.Key,
        Atts = gs.Distinct(),
    };