Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何基于2个属性筛选列表?_C#_Linq_List - Fatal编程技术网

C# 如何基于2个属性筛选列表?

C# 如何基于2个属性筛选列表?,c#,linq,list,C#,Linq,List,我的代码中有一个列表,需要根据两个条件过滤并返回特定行。所讨论的列表是数据库中的模型列表。每个模型上都有两个ID属性,一个是数据表中唯一的ID,另一个是用于标识组并可以重复的ID。我们称之为ID和GroupID。基本上,我希望生成的列表中每个GroupID只有一个,并且应该是具有最高数字标识的列表。例如: 输入: 这里需要做的是,首先按ID对modelList进行排序,然后按GroupID对列表项进行分组,然后使用最大ID值提取该项 var result = modelList.OrderByD

我的代码中有一个列表,需要根据两个条件过滤并返回特定行。所讨论的列表是数据库中的模型列表。每个模型上都有两个ID属性,一个是数据表中唯一的ID,另一个是用于标识组并可以重复的ID。我们称之为ID和GroupID。基本上,我希望生成的列表中每个GroupID只有一个,并且应该是具有最高数字标识的列表。例如:

输入:


这里需要做的是,首先按ID对modelList进行排序,然后按GroupID对列表项进行分组,然后使用最大ID值提取该项

var result = modelList.OrderByDescending(x => x.ID).GroupBy(x => x.GroupID).Select(x => x.First());
上面的查询将给出结果

var newModelList = modelList.GroupBy(ml => ml.GroupID)
.Select(g => new MyModel
{
    ID = g.OrderByDescending(x => x.ID).First().ID,
        GroupID = g.Key
}).ToList();
细节

1 GroupBy然后选择通过GroupID获取不同的项目

2在OrderByDescending之后首先获取最高ID

3 Select中的新MyModel只是为了明确显示投影。

您可以尝试使用

这将按GroupId对所有元素进行分组,并在其中一个组中选择Max ID。

使用LINQ:

var items = (from model in modelList
             group model by model.GroupID into modelGroup
             select modelGroup.Max(i => i.ID)).ToList();
请尝试以下代码:

List<MyModel> modelList = new List<MyModel>();
modelList.Add(new MyModel());
modelList.Add(new MyModel());
modelList.Add(new MyModel());
modelList.Add(new MyModel());

modelList[0].ID = 1; modelList[0].GroupID = 5;
modelList[1].ID = 2; modelList[1].GroupID = 5;
modelList[2].ID = 3; modelList[2].GroupID = 6;
modelList[3].ID = 4; modelList[3].GroupID = 6;

var list = from ml in modelList group ml by ml.ID into r select new { ID = r.Key, MaxGroupID = r.Max() };
这可能对你有帮助
modelList.GroupBymodel=>model.GroupId,g=>g.Id.Selectitem=>item.Max

这是您的解决方案:

var myData = models.GroupBy(model => model.GroupId)
                   .Select(group => group.OrderByDescending(model => model.Id).First());
或者你也可以这样做:

var myData = models.GroupBy(model => model.GroupId)
                   .Select(group => group.First(model => model.Id == group.Max(model1 => model1.Id)));

有趣的是,一把小提琴。

嗨,这是一个提示或提示,但不是答案。请你提供一个完整的答案好吗?用查询编辑。你能取消否决票吗?你的问题和你想要的结果不一致。你想要的是索引为1和3的模型,还是ID为1和3的模型?索引1和3,即ID为2和4。他希望每组的ID最高。那么是的,他想要的输出是有效的@CameronIs it?我看不到你能给我们看看吗@CameronTry是一个创建控制台应用程序,可以复制粘贴到Main方法中。谢谢如果您需要SQL语句,很抱歉,但这不是正确的位置。你用C标记你的问题!:-这不是我的问题:我只是在看,如果不是SQL,这是什么?这引起了我的注意,cz我需要更多的答案,这似乎是我在寻找的,我会在我这方面进行测试。虽然这段代码可能有助于解决问题,但提供关于为什么和/或如何回答这个问题的附加上下文将显著提高其长期价值。请在回答中添加一些解释。@oɔɯǝɹ感谢您的建议。编辑了答案。
List<MyModel> modelList = new List<MyModel>();
modelList.Add(new MyModel());
modelList.Add(new MyModel());
modelList.Add(new MyModel());
modelList.Add(new MyModel());

modelList[0].ID = 1; modelList[0].GroupID = 5;
modelList[1].ID = 2; modelList[1].GroupID = 5;
modelList[2].ID = 3; modelList[2].GroupID = 6;
modelList[3].ID = 4; modelList[3].GroupID = 6;

var list = from ml in modelList group ml by ml.ID into r select new { ID = r.Key, MaxGroupID = r.Max() };
var myData = models.GroupBy(model => model.GroupId)
                   .Select(group => group.OrderByDescending(model => model.Id).First());
var myData = models.GroupBy(model => model.GroupId)
                   .Select(group => group.First(model => model.Id == group.Max(model1 => model1.Id)));