【问题标题】:How to create multiply WHERE (and compare) with LINQ?如何使用 LINQ 创建乘法 WHERE(并进行比较)?
【发布时间】:2018-09-21 08:27:03
【问题描述】:

我有这个要求,它运作良好。主要问题是where filterIds.Contains(tags.UserTagId)部分。它会查找具有 OR 种类的所有标签。因此,如果我的 filterIds 值为 1、2、3,它会选择具有任何此标签的联系人。

 var result = (from conts in _context.Contacts
            where conts.CreatorUserId == _userManager.GetUserId(this.User) &&
                  (from tags in _context.ContactTags
                      where filterIds.Contains(tags.UserTagId)
                      select tags.ContactId).Contains(conts.ID)
            select new
            {
                conts.ID,  conts.Name, });

我需要找到所有具有 AND 类型的 ContactId。因此,如果联系人包含 ID 1、2 和 3,那就给我吧。就像找到所有具有所有这些标签的联系人一样。

 from tags in _context.ContactTags
            where filterIds[0] == tags.UserTagId 
and filterIds[1] == tags.UserTagId 
and filterIds[n] == tags.UserTagId
            select ...

甚至

 (from tags in _context.ContactTags
                where filterIds[0] == tags.UserTagId 

                select ...) where filterIds[1] == tags.UserTagId ...

怎么做?

【问题讨论】:

标签: c# sql .net linq


【解决方案1】:

用 Count =) 求解(修改为 Framwork LINQ)

 var result = _context.Contacts.Where(conts => conts.CreatorUserId == _userManager.GetUserId(this.User)
                              && conts.Tags.Where(t => filterIds.Contains(t.UserTagId)).Count() == filterIds.Count)
                .Select(conts=> new...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2022-12-15
    • 1970-01-01
    相关资源
    最近更新 更多