LINQ在课程项目上与众不同? [英] LINQ distinct on class item?

查看:60
本文介绍了LINQ在课程项目上与众不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这个问题与该问题类似,但我不适用于linq-to-sql,因此"let"不可用.

Note this question is similar this one except I'm not working with linq-to-sql, so the "let" is not usable.

基本上我可以选择类型

... .Select(c => new SampleClass { Id = c.Location.Name, Name = c.Location.Name }).Distinct().ToList()

在我刚有的时候就可以工作

which used to work when I just had

... .Select(c => c.Location.Name).Distinct().ToList()

我如何对SampleClass中的一个项目进行独特的调用?

How would I make a distinct call on one of the items within the SampleClass?

推荐答案

可以按键对项目进行分组,然后从组中选择要用作值的项目.我以FirstOrDefault为例:

You can group items by the key, and then select what item from the group you want to use as value. I use FirstOrDefault as an example:

... .Select(c => new SampleClass { Id = c.Location.Name, Name = c.Location.Name })
    .GroupBy(c => c.Id)
    .Select(group => group.FirstOrDefault())
    .ToList()

这篇关于LINQ在课程项目上与众不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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