自定义分组逻辑 [英] Custom grouping logic
本文介绍了自定义分组逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
让我说我有这个班级
公共类文章
{
public int ID {get; set;}
public string Name {get; set;}
public int State {get;设置;}
}
此测试数据
列表<物品> lst = new List< article>();
lst.Add(new article(){ID = 1,Name =" Item1",state = 0});
lst.Add(new article(){ID = 2,Name =" Item2",state = 0});
lst.Add(new article(){ID = 3,Name =" Item3",state = 1});
lst.Add(new article(){ID = 4,Name =" Item4",state = 1});
lst.Add(new article(){ID = 5,Name =" Item5",state = 2});
lst.Add(new article(){ID = 6,Name =" Item6",state = 2});
lst.Add(new article(){ID = 7,Name =" Item7",state = 3});
我想拥有一组3组的分组:
组1:其中state = 0或1
第2组: WHere state = 2
第3组:其中state = 3
这只是一个简单的测试用例,但我希望能够控制分组标准条件
谢谢。
解决方案
你好issam1975,
根据你的描述,我建议你可以将记录填入另一个对象,然后由它组成。您引用的以下代码。
class Program
{
static void Main(string [] args)
{
List< article> lst = new List< article>();
lst.Add(new article(){ID = 1,Name =" Item1",State = 0});
lst.Add(new article(){ID = 2,Name =" Item2",State = 0});
lst.Add(new article(){ID = 3,Name =" Item3",State = 1});
lst.Add(new article(){ID = 4,Name =" Item4",State = 1});
lst.Add(new article(){ID = 5,Name =" Item5",State = 2});
lst.Add(new article(){ID = 6,Name =" Item6",State = 2});
lst.Add(new article(){ID = 7,Name =" Item7",State = 3});
var dtos = lst.Select(t => new articleDto()
{
ID = t.ID,
Name = t.Name,
State = t.State,
GroupState =(t.State == 0 || t.State == 1)?0:t.State
})。ToList();
// group by here
var groupResult = dtos.GroupBy(t => t.GroupState).Select(t => new {key = t.Key,count = t .Count()});
Console.ReadLine();
}
}
公共类文章
{
public int ID {get;组; }
public string Name {get;组; }
public int State {get;组; }
}
公共类文章D
{
public int ID {get;组; }
public string Name {get;组; }
public int State {get;组; }
public int GroupState {get;组; }
}祝你好运,
张龙
hi everyone,
let's say that i have this class
public class article { public int ID {get; set;} public string Name {get; set;} public int State {get; set;} }
and this test data
List<article> lst = new List<article>(); lst.Add(new article(){ID= 1, Name="Item1", state=0}); lst.Add(new article(){ID= 2, Name="Item2", state=0}); lst.Add(new article(){ID= 3, Name="Item3", state=1}); lst.Add(new article(){ID= 4, Name="Item4", state=1}); lst.Add(new article(){ID= 5, Name="Item5", state=2}); lst.Add(new article(){ID= 6, Name="Item6", state=2}); lst.Add(new article(){ID= 7, Name="Item7", state=3});
i want to have a grouped collection with 3 groups :
Group 1: where state= 0 or 1
Group 2 : WHere state = 2
Group 3 : where state = 3
this just a simple test case but i want to be able to control the grouping criteria condition
thanks .
解决方案
Hi issam1975,
According to your description, I would suggest that you could fill the records into another object, then group by it. the following code for you reference.
class Program { static void Main(string[] args) { List<article> lst = new List<article>(); lst.Add(new article() { ID = 1, Name = "Item1", State = 0 }); lst.Add(new article() { ID = 2, Name = "Item2", State = 0 }); lst.Add(new article() { ID = 3, Name = "Item3", State = 1 }); lst.Add(new article() { ID = 4, Name = "Item4", State = 1 }); lst.Add(new article() { ID = 5, Name = "Item5", State = 2 }); lst.Add(new article() { ID = 6, Name = "Item6", State = 2 }); lst.Add(new article() { ID = 7, Name = "Item7", State = 3 }); var dtos = lst.Select(t => new articleDto() { ID = t.ID, Name = t.Name, State = t.State, GroupState = (t.State == 0 || t.State == 1) ? 0 : t.State }).ToList(); // group by here var groupResult = dtos.GroupBy(t => t.GroupState).Select(t => new { key = t.Key,count = t.Count() }); Console.ReadLine(); } } public class article { public int ID { get; set; } public string Name { get; set; } public int State { get; set; } } public class articleDto { public int ID { get; set; } public string Name { get; set; } public int State { get; set; } public int GroupState { get; set; } }Best regards,
Zhanglong
这篇关于自定义分组逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文