使用 LINQ 的唯一项目列表
我已经使用 LINQ 一段时间了,但似乎被困在有关唯一项目的问题上,我有以下列表:
List<Stock> stock = new List<Stock>();
它具有以下属性:字符串 ID 、字符串类型、字符串描述,示例:
public class Stock
{
public string ID { get; set; }
public string Type { get; set; }
public string Description { get; set; }
}
我想要有一个 LINQ 查询,它将按类型对库存中的项目进行分组,并将其作为新的库存列表返回(它必须与原始库存列表的类型相同)。
示例数据:
ID Type Description
----------------------------------------------
1 Kitchen Appliance Dishwasher
2 Living Room Television
3 Kitchen Appliance Washing Machine
4 Kitchen Appliance Fridge
...
我的 Linq 查询希望能够返回示例中的所有厨房用具。
所以我会将其作为“类型”传递到查询中,它将返回项目 1、3 和 4 从此示例列表中。
返回的此列表还必须是以下类型:List
。
本质上,我想要按类型列出唯一项的列表,有点像 SQL 不同查询,如何在 LINQ 中实现此目的?
替代解决方案也可以,但必须仅为 Silverlight/C# 客户端代码。
另一个澄清是,我也可能不提供参数“厨房电器”,可能只想要独特的参数,例如,它只会返回厨房电器和客厅一次,就像一个类别,无论该类型有多少个有。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
灵活的方法
使用
GroupBy
和ToDictionary
创建List
值在ToDictionary
的字典Stock>Type
属性上键入:然后您可以轻松访问类型本身以及任何给定类型的列表:
这种方法确实可以满足您的所有需求,因为我看到了。但对于其他一些选项,请参见下文。
替代(快速和肮脏)方法
您始终可以使用
Where
获取所需类型的项目,然后
ToList
将这些项目放入新的List
中:响应最后一部分:
在这里,您似乎想要完全不同的东西:基本上是
独特
。对于此功能,您基本上可以使用Soonts的答案( (可选)返回IEnumerable
而不是IEnumerable
),或者您可以将Distinct
与Select
以避免需要实现IEqualityComparer< ;库存>
(见下文)。更新
为了回应您的澄清,这是我的建议:两种方法,一种用于每个目的(单一责任原则):
Flexible approach
Use
GroupBy
andToDictionary
to create a dictionary ofList<Stock>
values keyed on theType
property:Then you can access the types themselves as well as a list for any given type quite easily:
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, thenToList
to put these items in a newList<Stock>
:In response to this last part:
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 anIEnumerable<tKey>
instead ofIEnumerable<tSource>
), or you could just leverageDistinct
in combination withSelect
to avoid the need to implement anIEqualityComparer<Stock>
(see below).Update
In response to your clarification, here's my recommendation: two methods, one for each purpose (Single Responsibility Principle):
听起来这是一个简单的Where 子句。
如果您严格想要源列表中包含的
Types
:string[] typesFound = stock.Select(s=>s.Type).Distinct().ToArray();
Sounds like it's a simple Where clause needed.
If you wanted strictly the
Types
contained in the source list:string[] typesFound = stock.Select(s=>s.Type).Distinct().ToArray();
我可能不明白这个问题,但这对我来说听起来像一个非常简单的 Linq 查询:
或者如果您更喜欢扩展方法语法:
我不明白的是您在这种情况下对 unique 和 unique 的使用。这些对象已经是唯一的,在我看来,您想要一个基本查询“给我所有厨房用具”。
I probably don't understand the question, but this sounds to me like a pretty simple Linq-query:
or if you prefer the extension-method syntax:
What I don't understand is your usage of distinct and unique in this context. The objects are already unique, and it seems to me that you want a basic query "give me all kitchen appliances".
您也可以按照这些思路做一些事情,在这种情况下,我将获取 lucene 结果,然后将 umbraco 节点拉出为匿名类型
You could also do something along these lines, in this case I am taking lucene results then pulling off umbraco nodes to an anonymous type