使用LINQ项目的唯一列表 [英] Unique list of items using LINQ

查看:164
本文介绍了使用LINQ项目的唯一列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用LINQ有一段时间了,但似乎被卡住的东西与问候独特的项目,我有folling列表:

 列表与LT;股票及GT;股票=新的List<股票及GT;(); 

这具有以下属性:字符串ID,字符串类型,字符串描述,例如:



 公共类股票
{
公共字符串ID {搞定;组; }
公共字符串类型{搞定;组; }
公共字符串描述{搞定;组; }
}



我想有一个LINQ查询,将通过集团股票的项目。其类型与返回此作为股票的新列表(它必须是相同的类型与原始库存列表)



实施例的数据:

  ID类型说明
------------------------- ---------------------
1个厨房电器洗碗机
2客厅电视
3厨房电器洗衣机
4厨房电器冰箱

...



我的Linq查询希望能够返回所有的厨房电器为例。结果
所以我会通过这是一个型到查询,它会返回项目1,3和4
从这个例子列表



这个列表中返回也必须类型:列表<股票>



基本上我想按类型独特的项目清单,有点像SQL不同的查询,我怎么在LINQ实现这一目标?结果
替代的解决方案是好的,但必须的Silverlight / C#客户端唯一的代码。



又一个澄清的是,我也可能不提供参数厨房电器,并可能只是希望独特的人,例如,它会返回厨房电器和客厅各一次只有一种像一个类别,无论有多少类型有。


解决方案的

灵活的方式



使用的 GROUPBY ToDictionary 创建列表℃的字典;股票>键控上的键入值C $ C>属性:

  VAR appliancesByType =股票
.GroupBy(项目=>项目。形式)
.ToDictionary(GRP => grp.Key,GRP => grp.ToList());



然后就可以访问该类型本身以及对于任何给定类型很容易的列表:

  //名单的独特类型的名称仅
名单,LT;字符串> stockTypes = appliancesByType.Keys.ToList();

//或:每种类型的
名单,下一只股票的项目清单;股票及GT; exampleStocks = appliancesByType
。选择(KVP = GT; kvp.Value [0])
.ToList();

//列表库存物品给定类型
名单,LT的;股票> kitchenAppliances = appliancesByType [厨房电器];

这办法真的需要所有的需要小心,因为我看到了。但对于一些其他的选择,请参见下面



备用(快速和放脏)的方式



您可以随时只需使用 其中, 拿到项目你想要的类型,然后 了ToList 把这些项目在新的列表<股票>

 清单<股票及GT; kitchenAppliances =股票
。凡(项目=>项目。形式==厨房电器)
.ToList();






在响应于这个最后部分>


又一个澄清的是,我
也可能不提供的参数
厨房电器,并可能只是想
中的唯一的人,例如它将
返回厨房电器和生活
室各一次才有点像
类,无论有多少
型有。




在这里,你似乎想完全不同的东西:// MSDN:基本上是的 鲜明 。对于此功能,您可以用基本 Soonts的回答(可选,返回一个的IEnumerable< TKEY> 而不是的IEnumerable< tSource> ),或者你可以只杠杆鲜明 选择组合 以避免需要实施的IEqualityComparer<股票方式> (见下文)



< HR>

更新



在响应你的澄清,这是我的建议:两个方法,一个用于各种用途(的单一职责原则):

  //这将返回具有特定类型
静态列表<所有股票对象的列表;股票> GetItemsForType(字符串类型)
{
股票的收益
。凡(项目=>项目。形式==型)
.ToList();
}

//这将返回所有类型的值(无重复)
静态列表<的名称列表;串> GetStockTypes()
{
股票的收益
。选择(项目=>项目。形式)
.Distinct()
.ToList();
}


I've been using LINQ for a while now, but seem to be stuck on something with regards to Unique items, I have the folling list:

List<Stock> stock = new List<Stock>();

This has the following Properties: string ID , string Type, string Description, example:

public class Stock
{
    public string ID { get; set; }
    public string Type { get; set; }
    public string Description { get; set; }
}

I want to have a LINQ query that will group the items in Stock by their Type and return this as a new List of Stock (it has to be the same type as the original Stock List).

Example Data:

 ID   Type                 Description
----------------------------------------------
  1   Kitchen Appliance    Dishwasher  
  2   Living Room          Television  
  3   Kitchen Appliance    Washing Machine  
  4   Kitchen Appliance    Fridge  

...

My Linq query wants to be able to return all the Kitchen Appliances for Example.
So I would pass this as a "type" into the query and it would return the items 1, 3 and 4 from this example list.

This list returned must also be of type: List<Stock>.

Essentially I want a list of the unique items by type, kind of like an SQL Distinct query, how do I achieve this in LINQ?
Alternative solutions are fine but must be Silverlight / C# client code only.

Just another clarification is that I also may not provide the parameter "Kitchen Appliance" and may just want the unique ones, for example It would return Kitchen Appliance and Living Room once each only to kind of like a category no matter how many of that Type there are.

解决方案

Flexible approach

Use GroupBy and ToDictionary to create a dictionary of List<Stock> values keyed on the Type property:

var appliancesByType = stock
    .GroupBy(item => item.Type)
    .ToDictionary(grp => grp.Key, grp => grp.ToList());

Then you can access the types themselves as well as a list for any given type quite easily:

// List of unique type names only
List<string> stockTypes = appliancesByType.Keys.ToList();

// Or: list of one stock item per type
List<Stock> exampleStocks = appliancesByType
    .Select(kvp => kvp.Value[0])
    .ToList();

// List of stock items for a given type
List<Stock> kitchenAppliances = appliancesByType["Kitchen Appliance"];

This approach really takes care of all your needs, as I see it. But for some other options, see below.

Alternate (quick & dirty) approach

You can always just use Where to get the items of the type you want, then ToList to put these items in a new List<Stock>:

List<Stock> kitchenAppliances = stock
    .Where(item => item.Type == "Kitchen Appliance")
    .ToList();


In response to this last part:

Just another clarification is that I also may not provide the parameter "Kitchen Appliance" and may just want the unique ones, for example It would return Kitchen Appliance and Living Room once each only to kind of like a category no matter how many of that Type there are.

Here, you seem to be wanting something completely different: basically the behavior provided by Distinct. For this functionality, you could essentially go with Soonts's answer (optionally, returning an IEnumerable<tKey> instead of IEnumerable<tSource>), or you could just leverage Distinct in combination with Select to avoid the need to implement an IEqualityComparer<Stock> (see below).


Update

In response to your clarification, here's my recommendation: two methods, one for each purpose (Single Responsibility Principle):

// This will return a list of all Stock objects having the specified Type
static List<Stock> GetItemsForType(string type)
{
    return stock
        .Where(item => item.Type == type)
        .ToList();
}

// This will return a list of the names of all Type values (no duplicates)
static List<string> GetStockTypes()
{
    return stock
        .Select(item => item.Type)
        .Distinct()
        .ToList();
}

这篇关于使用LINQ项目的唯一列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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