LINQ独特查询 [英] LINQ Distinct Query

查看:69
本文介绍了LINQ独特查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#应用程序,该应用程序加载了一个称为任务"的CLR对象列表.每个任务具有以下属性:

I have a C# application that loads a List of CLR objects called "Tasks". Each Task has the following properties:

public int ID { get; set; }
public int TypeID { get; set; }
public string TypeName { get; set; }
public string Name { get; set; }

我正在尝试在此列表中找到任务的唯一类型.我以为我会尝试使用LINQ来做到这一点.但是,我不知道该怎么做.我怎么说,给我这个列表中所有唯一的TypeID和TypeName?目前,我正在尝试以下操作,这使我无处可去.实际上,它甚至可以编译.

I am trying to find the unique types of tasks in this list. I thought I would try to use LINQ to do it. However, I cannot figure out how to do it. How do I say give me all of the unique TypeID and TypeName in this list? Currently I'm trying the following which is getting me no where. In fact, it wound even compile.

var uniqueTasks = allTasks.Distinct(p => p.TypeID);

谢谢

推荐答案

让我确保我理解问题所在:您有一个名为allTask​​s的任务序列,并且您想要一个无重复的序列 所有ID中,是吗?

Let me make sure I understand the problem: you have a sequence of tasks called allTasks, and you would like a sequence without duplicates of all the ids, yes?

将任务序列转换为ID序列:

Transform the sequence of tasks into a sequence of ids:

var ids = allTasks.Select(p=>p.TypeId);

现在您有了ID序列.您希望从该序列中过滤出重复项:

Now you have a sequence of ids. You wish to filter out the duplicates from that sequence:

var distinctIds = ids.Distinct();

您已经完成.那是你的追求吗?

And you're done. Is that what you were after?

这篇关于LINQ独特查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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