【叫我猫爷_】Linq多字段分组. 求Linq高手支招..

叫我猫爷_ 2014-01-22 10:32:14

public class Student
{
public string Name { get; set;}
public string Code { get; set;}
public List<ClassMate> MateList { get; set;}
}

public class ClassMate
{
public string Name { get; set;}
public string Remark { get; set;}
public string Address { get; set;}
}



现在我想要的结果呢. 就是根据ClassMate类的Name和Address分组.. 好像组合主键那样子吧..
这应该要怎么写.? 如果是单字段分组我会,多字段我就不知道了..
单字段我是这样写的..

IEnumerable<IGrouping<string, ClassMate>> query = stu.MateList.GourpBy(g => g.Name, g => g);


求Linq高手支招..
...全文
252 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2014-01-22
  • 打赏
  • 举报
回复
引用 7 楼 wyd1520 的回复:
private class ClassMateComparer : IEqualityComparer<ClassMate> { public bool Equals(ClassMatex, ClassMate y) { return x.Name== y.Name && x.Address == y.Address ; } public int GetHashCode(ClassMate obj) { if (obj == null) { return 0; } else { return obj.ToString().GetHashCode(); } } } stu.MateList.GroupBy(o => new ClassMateComparer());
按照你的写法,应该用Distinct
本拉灯 2014-01-22
  • 打赏
  • 举报
回复
private class ClassMateComparer : IEqualityComparer<ClassMate> { public bool Equals(ClassMatex, ClassMate y) { return x.Name== y.Name && x.Address == y.Address ; } public int GetHashCode(ClassMate obj) { if (obj == null) { return 0; } else { return obj.ToString().GetHashCode(); } } } stu.MateList.GroupBy(o => new ClassMateComparer());
wg5945 2014-01-22
  • 打赏
  • 举报
回复
引用 3 楼 lerbornjames 的回复:
[quote=引用 1 楼 u010006337 的回复:] 百度啊 有一大把资料啊
回水区踢你蛋蛋去.. 就是百度不到才问的,你这2货.. [/quote] 目测要火~~
threenewbee 2014-01-22
  • 打赏
  • 举报
回复
stu.MateList.GroupBy(x => new { x.Name, x.Address })
叫我猫爷_ 2014-01-22
  • 打赏
  • 举报
回复
引用 2 楼 wpfLove 的回复:
不用sql那种写法.. 而且.. 请看清题目,
叫我猫爷_ 2014-01-22
  • 打赏
  • 举报
回复
引用 1 楼 u010006337 的回复:
百度啊 有一大把资料啊
回水区踢你蛋蛋去.. 就是百度不到才问的,你这2货..
小猪八Q 2014-01-22
  • 打赏
  • 举报
回复
http://code.msdn.microsoft.com/LINQ-to-DataSets-Grouping-c62703ea 这里有

 public void Linq43() 
    { 
        List<Customer> customers = GetCustomerList(); 
      
        var customerOrderGroups = 
            from c in customers 
            select 
                new 
                { 
                    c.CompanyName, 
                    YearGroups = 
                        from o in c.Orders 
                        group o by o.OrderDate.Year into yg 
                        select 
                            new 
                            { 
                                Year = yg.Key, 
                                MonthGroups = 
                                    from o in yg 
                                    group o by o.OrderDate.Month into mg 
                                    select new { Month = mg.Key, Orders = mg } 
                            } 
                }; 
      
        ObjectDumper.Write(customerOrderGroups, 3); 
    } 
___________小P 2014-01-22
  • 打赏
  • 举报
回复
百度啊 有一大把资料啊
wg5945 2014-01-22
  • 打赏
  • 举报
回复
哎,难道LZ非要写成List<List<ClassMate>> cMateList;这种样子的才行? List<List<ClassMate>> cMateList = stu.MateList.GroupBy(x => new { x.Name, x.Address }).Select(y => y.ToList()).ToList(); 这下你满足了吧? 有意义么?
本拉灯 2014-01-22
  • 打赏
  • 举报
回复
引用 20 楼 lerbornjames 的回复:
[quote=引用 16 楼 wg5945 的回复:] [quote=引用 14 楼 lerbornjames 的回复:] [quote=引用 13 楼 wg5945 的回复:]

var q = stu.MateList.GroupBy(x => new { x.Name, x.Address }).Select(y => new { y.Key.Name, y.Key.Address, Count = y.Count() });
foreach (var item in q)
{
    Console.WriteLine("Name: {0} Address: {1} Count: {2}", item.Name, item.Address, item.Count);
}
[quote=引用 10 楼 lerbornjames 的回复:] [quote=引用 5 楼 caozhy 的回复:] stu.MateList.GroupBy(x => new { x.Name, x.Address })
var q = stu.MateList.GroupBy(x=>new{x.Name,x.Address}); foreach(ClassMate item in q) //这里报错,不能强制转换类型.. { }[/quote][/quote] 那Remark呢..? 怎么拿..? 肯定是要拿那个对象.. 不能只拿到某个属性. [/quote] foreach (var item in q) { Console.WriteLine("Key: {0} ", item.Key); foreach (var cm in item) { Console.WriteLine("Remark: {0} ", cm.Remark); } } 这样就可以了~~[/quote] 这个是可以,但是又引发了一个问题.. 你怎么知道有多少个分组,每个分组的数据是什么..[/quote] var q = stu.MateList.GroupBy(x=>new{x.Name,x.Address}); q.Count() 就是多少组。 foreach (var item in q) { foreach (var cm in item) <--这个就是每组的数据。。 { } }
叫我猫爷_ 2014-01-22
  • 打赏
  • 举报
回复
引用 16 楼 wg5945 的回复:
[quote=引用 14 楼 lerbornjames 的回复:] [quote=引用 13 楼 wg5945 的回复:]

var q = stu.MateList.GroupBy(x => new { x.Name, x.Address }).Select(y => new { y.Key.Name, y.Key.Address, Count = y.Count() });
foreach (var item in q)
{
    Console.WriteLine("Name: {0} Address: {1} Count: {2}", item.Name, item.Address, item.Count);
}
[quote=引用 10 楼 lerbornjames 的回复:] [quote=引用 5 楼 caozhy 的回复:] stu.MateList.GroupBy(x => new { x.Name, x.Address })
var q = stu.MateList.GroupBy(x=>new{x.Name,x.Address}); foreach(ClassMate item in q) //这里报错,不能强制转换类型.. { }[/quote][/quote] 那Remark呢..? 怎么拿..? 肯定是要拿那个对象.. 不能只拿到某个属性. [/quote] foreach (var item in q) { Console.WriteLine("Key: {0} ", item.Key); foreach (var cm in item) { Console.WriteLine("Remark: {0} ", cm.Remark); } } 这样就可以了~~[/quote] 这个是可以,但是又引发了一个问题.. 你怎么知道有多少个分组,每个分组的数据是什么..
叫我猫爷_ 2014-01-22
  • 打赏
  • 举报
回复
引用 17 楼 caozhy 的回复:
stu.MateList.GroupBy(x => new { x.Name, x.Address })
var q = stu.MateList.GroupBy(x=>new{x.Name,x.Address}.Select(x => x.First())); foreach(ClassMate item in q) //这里报错,不能强制转换类型.. { }[/quote] 这个可以拿到分组数量和第一个元素,如果是用SelectMany的话就感觉不出已经分组了. 有没有办法可以拿到一个二维数组. 这样数据便于操作.. List<List<ClassMate>> cMateList; 这样就既能知道有多少个分组,和每个分组有什么数据.
烟波钓 2014-01-22
  • 打赏
  • 举报
回复

stu.MateList.GroupBy(x => new { x.Name, x.Address })

var q = stu.MateList.GroupBy(x=>new{x.Name,x.Address}.Select(x => x.First())).ToList();
foreach(var item in q)
{
}
threenewbee 2014-01-22
  • 打赏
  • 举报
回复
stu.MateList.GroupBy(x => new { x.Name, x.Address })[/quote] var q = stu.MateList.GroupBy(x=>new{x.Name,x.Address}.Select(x => x.First())); foreach(ClassMate item in q) //这里报错,不能强制转换类型.. { }
wg5945 2014-01-22
  • 打赏
  • 举报
回复
引用 14 楼 lerbornjames 的回复:
[quote=引用 13 楼 wg5945 的回复:]

var q = stu.MateList.GroupBy(x => new { x.Name, x.Address }).Select(y => new { y.Key.Name, y.Key.Address, Count = y.Count() });
foreach (var item in q)
{
    Console.WriteLine("Name: {0} Address: {1} Count: {2}", item.Name, item.Address, item.Count);
}
[quote=引用 10 楼 lerbornjames 的回复:] [quote=引用 5 楼 caozhy 的回复:] stu.MateList.GroupBy(x => new { x.Name, x.Address })
var q = stu.MateList.GroupBy(x=>new{x.Name,x.Address}); foreach(ClassMate item in q) //这里报错,不能强制转换类型.. { }[/quote][/quote] 那Remark呢..? 怎么拿..? 肯定是要拿那个对象.. 不能只拿到某个属性. [/quote] foreach (var item in q) { Console.WriteLine("Key: {0} ", item.Key); foreach (var cm in item) { Console.WriteLine("Remark: {0} ", cm.Remark); } } 这样就可以了~~
wg5945 2014-01-22
  • 打赏
  • 举报
回复
引用 9 楼 lerbornjames 的回复:
[quote=引用 5 楼 caozhy 的回复:] stu.MateList.GroupBy(x => new { x.Name, x.Address })
曹版,这种多列的我怎么用循环来拿结果..? IEnumerable<IGrouping<string, ClassMate>> query = stu.MateList.GourpBy(g => g.Name, g => g); List<ClassMate> qMate = query.Where(w=>w.Key="xxx"); 这种我可以 foreach(ClassMate item in qMate) { xxx=xxxx..... } 这样.. 但是如果是多列的我不知道怎么拿结果.. 我用debug是看到有我想要的结果了.. 但是拿不出.. [/quote] item 不是 ClassMate ,ClassMate 在 item 里面,还有一层循环 foreach (ClassMate item in q.SelectMany(x => x)) { Console.WriteLine("Name: {0} Address: {1} Remark: {2} ", item.Name, item.Address, item.Remark); } 这样子item 才是 ClassMate
叫我猫爷_ 2014-01-22
  • 打赏
  • 举报
回复
引用 13 楼 wg5945 的回复:

var q = stu.MateList.GroupBy(x => new { x.Name, x.Address }).Select(y => new { y.Key.Name, y.Key.Address, Count = y.Count() });
foreach (var item in q)
{
    Console.WriteLine("Name: {0} Address: {1} Count: {2}", item.Name, item.Address, item.Count);
}
[quote=引用 10 楼 lerbornjames 的回复:] [quote=引用 5 楼 caozhy 的回复:] stu.MateList.GroupBy(x => new { x.Name, x.Address })
var q = stu.MateList.GroupBy(x=>new{x.Name,x.Address}); foreach(ClassMate item in q) //这里报错,不能强制转换类型.. { }[/quote][/quote] 那Remark呢..? 怎么拿..? 肯定是要拿那个对象.. 不能只拿到某个属性.
wg5945 2014-01-22
  • 打赏
  • 举报
回复

var q = stu.MateList.GroupBy(x => new { x.Name, x.Address }).Select(y => new { y.Key.Name, y.Key.Address, Count = y.Count() });
foreach (var item in q)
{
    Console.WriteLine("Name: {0} Address: {1} Count: {2}", item.Name, item.Address, item.Count);
}
引用 10 楼 lerbornjames 的回复:
[quote=引用 5 楼 caozhy 的回复:] stu.MateList.GroupBy(x => new { x.Name, x.Address })
var q = stu.MateList.GroupBy(x=>new{x.Name,x.Address}); foreach(ClassMate item in q) //这里报错,不能强制转换类型.. { }[/quote]
完美-态度 2014-01-22
  • 打赏
  • 举报
回复
叫我猫爷_ 2014-01-22
  • 打赏
  • 举报
回复
引用 5 楼 caozhy 的回复:
stu.MateList.GroupBy(x => new { x.Name, x.Address })
var q = stu.MateList.GroupBy(x=>new{x.Name,x.Address}); foreach(ClassMate item in q) //这里报错,不能强制转换类型.. { }
加载更多回复(1)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧