【问题标题】:LINQ contain each element of all groups?LINQ 包含所有组的每个元素吗?
【发布时间】:2014-02-11 04:26:43
【问题描述】:

我有这个匹配条件:

            var matchConditions = new long[][] 
                { 
                    new long[] { 109, 145 }, //color Id
                    new long[] { 202 }, // province Id
                    new long[] { 303, 309, 317, 318 }, //options Id
                };

它们中的每一个都是不同组的 id,例如第一个嵌套数组用于颜色。
我需要知道哪些产品符合颜色 id 109 或 145,第二个是其他产品。
我的意思是我想获得满足每个组中任何项目的所有产品,

{color 109 145 - - 省 202 - - 选项 303 309 317 318}

我试过了:

matchConditions.ToList().ForEach(x => x.Any(j => adAdFields.Select(co => co.listFieldId).Contains(j)))

matchConditions.All(x => x.Any(j => adAdFields.Select(co => co.listFieldId).Contains(j)))

但它们都不起作用

编辑: 在此之前我有这个查询:

var adsWithRelevantadFields =
from adField in cwContext.tblAdFields
join ads in cwContext.tblAds on adField.adId equals ads.id
where (prvId == 0 && ads.tabId == tabId) ||
(prvId != 0 & ctsId.Value == 0 && ads.provinceId == prvId & ads.tabId == tabId) ||
(prvId != 0 & ctsId.Value > 0 && ads.provinceId == prvId & ads.cityId == ctsId.Value & ads.tabId == tabId)
where ads.endDate >= theToDay
where ads.conditionId == 1
where ads.payed == true                                                       
group adField by adField.adId into adAdFields                                 
where searchIds.All(i => adAdFields.Select(co => co.listFieldId).Contains(i))

这很好用,但现在我需要搜索更多按我展示的方式分组的选项,所以我需要在查询末尾添加“&&”来搜索这些新项目,这是我的问题。

EDIT2
假设我现在有一些列表(颜色,谁生产,选项,哪里,......)当我想添加一个新产品时,以这些格式添加适当的属性(productId,attributeId),例如我有(1,109 - 1,202 - 1, 303 ...) 和 (2,109 - 2,202 - 2,318...) ...

所以当我对每个产品(真实的广告)进行分组时,我只需要检查哪些产品 grop:
{color 109 145 - - 省 202 - - 选项 303 309 或 317 318}

【问题讨论】:

  • 你能告诉我们产品类的结构吗?
  • matchConditions.Length 总是 == 3 吗?
  • 我更新了我的问题,广告字段按我的产品详细信息分组
  • @mrlucmorin,不,它是动态的。
  • @mohammadadibi 所以它总是 3 的倍数吗?我正在尝试了解您的设置。

标签: c# linq


【解决方案1】:

这样的事情怎么样?

adAdFields.Where(x => matchConditions[0].Contains(x.colorId)
                  &&  matchConditions[1].Contains(x.provinceId)
                  && matchConditions[2].Contains(x.optionsId))
          .ToList();

【讨论】:

  • 谢谢,但我的匹配条件是可变的,可以超过3个,这只是一个例子
【解决方案2】:

我认为您需要细化您的查询,如何构建此查询 - 一步一步,在这种情况下,您可以提高代码的可读性并检查所有条件的可用性。

看看这个:

class Program
{
    static void Main(string[] args)
    {
        var adAdFields = new List<AdAdField>
        {
            new AdAdField {colorId = 109, optionsId = 303, provinceId = 202},
            new AdAdField {colorId = 145, optionsId = 309, provinceId = 2},
            new AdAdField {colorId = 3, optionsId = 317, provinceId = 3},
            new AdAdField {colorId = 4, optionsId = 318, provinceId = 4}
        }.AsQueryable();

        var matchConditions = new long[][] 
            { 
                new long[] { 109, 145 }, //color Id
                new long[] { 202 }, // province Id
                new long[] { 303, 309, 317, 318 }, //options Id
            };

        var result1 = adAdFields.Where(x => matchConditions[0].Contains(x.colorId)
                              && matchConditions[1].Contains(x.provinceId)
                              && matchConditions[2].Contains(x.optionsId)).ToList();

        var query = adAdFields;

        if (matchConditions[0].Length > 0)
            query = query.Where(x => matchConditions[0].Contains(x.colorId));

        if (matchConditions[1].Length > 0)
            query = query.Where(x => matchConditions[1].Contains(x.provinceId));

        if (matchConditions[2].Length > 0)
            query = query.Where(x => matchConditions[2].Contains(x.optionsId));
        //below will be other possible conditions....


        var result2 = query.ToList();
        //result2 and result1 ARE SAME!!!
    }
}

public class AdAdField
{
    public int colorId { get; set; }
    public int provinceId { get; set; }
    public int optionsId { get; set; }
}

IQueriable 会为查询添加条件,最终在查询结束时调用.ToList(),它会强制 orm 生成适当的 sql。直到此时您才刚刚构建查询。

【讨论】:

  • 我的属性(颜色,选项,...)是动态的。我想我必须 groupby matchconditions 但是如何,我不知道。
【解决方案3】:

您可以使用动态 linq 构造一个类似于@Selman22 答案中的 linq 查询,条件数量不同。基本上,一个 linq 查询是由一个带有表达式的树组成的。 动态 LINQ 意味着您可以动态地构建树,从而为您提供更多创建查询的能力。

您可以使用 PredicateBuilder 来执行此操作,或使用现成的库,例如 dynamic-query-library。 见:

http://www.codeproject.com/Articles/231706/Dynamic-query-with-Linq

http://msdn.microsoft.com/en-us/library/bb882637.aspx

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

【讨论】:

  • 我无法理解这些链接,你能指导我更多吗,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-12
  • 2013-11-26
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多