日期:2014-05-20  浏览次数:20844 次

Linq分组查询的问题,求指教
List<string[]> allList = msgList as List<string[]>;
List<string[]> takeList = new List<string[]>();
var qurey = from p in allList group p by p[0] into grouping select new { grouping.Key, maxd = from p2 in grouping where p2[4] == grouping.Max(x => x[4]) select p2 };
foreach (var val in qurey)
{
   foreach (string[] msg in val.maxd)
   {
       takeList.Add(msg);
   }
}
我想根据列表项数组的第一列分组然后查出每组中第五列最大的项组成的结果列表,这是我仿照http://msdn.microsoft.com/zh-tw/magazine/bb386972(VS.90).aspx写的,功能可以实现,但是我看见那两个foreach总觉得不大合适,应该还有更好的办法吧,求高手指教?
LINQ asp.net

------解决方案--------------------
list的每一项是string[],你要遍历数组每一项,,肯定要用遍历
你可以这样改改

List<string[]> allList = new List<string[]>() { new string[] { "1", "list11", "col1", "t1", "1" }, new string[] { "2", "list2", "col2", "t2", "2" }, new string[] { "1", "list12", "col1", "t3", "12" }, new string[] { "3", "list3", "col1", "t4", "3" }, new string[] { "1", "list13", "col1", "t5", "13" } };
            List<string[]> takeList = new List<string[]>();
            var qurey = from p in allList
                        group p by p[0] into grouping
                        let max = grouping.Max(y => y[4])
                        select new