分组依据 - Linq查询 [英] Group By - Where Linq Query

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

问题描述

大家好,



我有一个List< t>我需要对此列表进行一些操作。我想要的是我想通过这两个列的元素分组,并用一定的值过滤它们。



所以我知道如何按两个列分组as:

Hello all,

I have a List<t> and I need to do some manipulation to this list. What I want is I want to group by this list elements by two columns and filter them with a certain value.

So I know how to group by by two colums such as:

myList.GroupBy(g => new{ g.DB, g.Type }).Select(g => g.First())



但是这里虽然我想要类型等于C

我不知道,但我的目标是编码类似的东西:


But here while group by I want the Type equals to "C"
I dont know but my goal is to code something like:

myList.GroupBy(g => new{ g.DB, g.Type.Equals("C") }).Select(g => g.First())



-OR-


-OR-

myList.GroupBy(g => new{ g.DB, g.Type }).Where(g => g.Type = "C").Select(g => g.First())





但是我无法想象出来。有人可以帮我完成这个查询。在此先感谢。



but I can not figure it out. Can someone help me complete this query. Thanks in advance.

推荐答案

试试这个:

Try this:
myList.GroupBy(g=> new {g.DB, g.Type}).Where(g=>g.Type=="C").FirstOrDefault();

我相信First是你列表中的一个字段。

I believe First is one of the field inside your list.
var newlist=myList.Where(g=>g.Type.Equals("C"))
.GroupBy(g => new{ g.DB, g.Type})
.Select(g => g.First());


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

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