开发者

How do I add which OR condition was met to my result

开发者 https://www.devze.com 2023-02-04 11:21 出处:网络
In the fo开发者_C百科llowing LINQ query I\'m returning people that meet some criteria. In the criteria, I have an OR condition. How do I return which of the OR conditions the person met? I\'d like to

In the fo开发者_C百科llowing LINQ query I'm returning people that meet some criteria. In the criteria, I have an OR condition. How do I return which of the OR conditions the person met? I'd like to include a x.AttId in the .Select statement. Each person can have many AttIds assigned to them at the same time.

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)


You can do the following:

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)


Here's an option if you wish to know the distinct set of AttID that match.

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(),
    };
0

精彩评论

暂无评论...
验证码 换一张
取 消