如何将满足哪个 OR 条件添加到我的结果中

发布于 2024-10-12 02:19:40 字数 556 浏览 2 评论 0原文

在下面的 LINQ 查询中,我返回满足某些条件的人员。在标准中,我有一个 OR 条件。如何返回此人满足了哪些 OR 条件?我想在 .Select 语句中包含 x.AttId。每个人可以同时分配多个 AttId。

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)

In the following 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

三人与歌 2024-10-19 02:19:40

您可以执行以下操作:

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)

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)
千纸鹤带着心事 2024-10-19 02:19:40

如果您想知道匹配的 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(),
    };

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