C#中找到一个List&LT所有匹配的项目;项目> [英] C# find all matching Items from a List<Item>

查看:720
本文介绍了C#中找到一个List&LT所有匹配的项目;项目>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我们有一个列表<产品> 然后在列表中每个产品有许多列表<类型>

 公共类产品{
公众诠释编号{得到:集;}
公共字符串名称{得到:集;}
公开名单<类型>类型{获取;集;}
}

公共类类型{
公众诠释标识{获取;集;}
公共字符串名称{;设置;}
}

我创建了一个产品列表后,我需要给他们组的类型和然后找到所有属于每个类型。我觉得我应该尝试 LINQ 这一点。以下是我迄今所做的,不过好像不是正确的方式来完成这项工作。可能有人能帮助我。

  VAR产品列表=新的List<产品及GT;(); 
//添加产品类型为每个

变种productTypesList =新词典< INT,串>();

的foreach(在产品列表VAR P)
{
VAR pTypes = p.Types;
的foreach(在$ B $ VAR PTYPE b pTypes.Where(X =>!productTypesList .ContainsKey(x.Id)))
{
productTypesList.Add(ptype.Id,p型。名称);
}
}



然后我试图寻找筛选

 的foreach(在productTypesList变种T)
{
VAR匹配= productsList.FindAll(..... ...);
//从这里,我需要找到所有属于类型(t.id)

产品:(matches.Count大于0)
{
//做财产以后这里
}
}


解决方案

下你想要做什么:



从T

  VAR productsPerType = 
在products.SelectMany(
P => p.Types,(p,T)=>新建{产品= p,TYPEID = t.Id})
T组由t.TypeId
到G
。选择所需的新{g.Key,产品= g.Select(X => x.Product)};



首先,你做了的SelectMany 来获得所有类型的产品里面的一个列表。对于每一类你还记得类型标识和相应的产品:

 从吨products.SelectMany(
P => ; p.Types,(p,T)=>新建{产品= p,TYPEID = t.Id})

每个 T 现在包含一个类型ID和一个产品一个匿名对象。接下来,您将这些对象按类型ID。现在我们有一组产品为每种类型的ID



给你举个例子,假设你有以下的产品和类型:

 产品A  -  1型,2,3 
产品b - 类型1
产品C - 类型1,3



的SelectMany 给出了如下的中间结果:

  1,A 
2,A
3,A
1,b
1,C
3,C

我们组这个结果按类型ID,所以我们得到以下几组:

  1,{A,b,C} 
2,{A}
3 {A,C}

这就是你想要的结果。


Let's say we have a List<Product> and each product item in the list has many List<Type>

public class Product{
public int Id {get:set;}
public string Name {get:set;}
public List<Type> Types {get;set;}
}

public class Type{
public int Id {get;set;}
public string Name{get;set;}
}

After I create a product list, I need to group them by the type and then find all the belongs to each type. I think I should try LINQ for this. Here is what I have done so far, but seems not the right way to get the job done. May be someone can help me.

var productsList = new List<Product>();
//Adding products and types for each of them

var productTypesList = new Dictionary<int, string>();

 foreach (var p in productsList)
                            {
                                var pTypes = p.Types;
                                foreach (var ptype in
                                    pTypes.Where(x=> !productTypesList .ContainsKey(x.Id)))
                                {
                                    productTypesList.Add(ptype.Id, ptype.Name);
                                }
                            }

Then I'm trying to search Like this

foreach (var t in productTypesList)
{
var matches = productsList.FindAll(........); 
// from here I need to find all the product which belongs to type (t.id)

if (matches.Count > 0)
{
//Do somthing here
}
}

解决方案

The following does what you want:

var productsPerType =
    from t in products.SelectMany(
        p => p.Types, (p, t) => new { Product = p, TypeId = t.Id })
    group t by t.TypeId
    into g
    select new { g.Key, Products = g.Select(x => x.Product) };

First, you do a SelectMany to get a list of all types inside the products. For each type you remember the type id and the corresponding product:

from t in products.SelectMany(
    p => p.Types, (p, t) => new { Product = p, TypeId = t.Id })

Each t is now an anonymous object containing a type id and a product. Next, you group these objects by type id. Now we have a group of products for each type id.

To give you an example, suppose you have the following products and types:

Product A -- Types 1, 2, 3
Product B -- Types 1
Product C -- Types 1, 3

The SelectMany gives the following intermediate result:

1, A
2, A
3, A
1, B
1, C
3, C

We group this result by type id so we get the following groups:

1, { A, B, C }
2, { A }
3, { A, C }

And this is the result you wanted.

这篇关于C#中找到一个List&LT所有匹配的项目;项目&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆