c# List 集合数据求解 求思路算法

你采不起的野花 2017-05-19 12:40:57
后台查询出的结果不太理想,大概是
//claNo=1, stuNo=2],[claNo=1, stuNo=3],
//[claNo=1, stuNo=4].....
这种格式的。我想把它转换为 下面的deparArrytModel格式,
//clano=1, stuNo=2,3,4]....

搞了两天还是不会,特来求解。
大牛跪求,小弟已经是想破脑袋了。

List<departModel> = this.Cmssever.getstu();//获取到的数据 上面那几行

deparArrytModel deparArry=new deparArrytModel();



public class departModel{
public int claNo{get;set;}
public int stuNo{get;set;}
}

public class deparArrytModel{
public int claNo{get;set;}
public List<int> stuNo{get;set;}
}
...全文
213 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
谢谢各位版主大神,各位的方法都有用,谢谢3L给出的代码,很厉害。
正怒月神 2017-05-19
  • 打赏
  • 举报
回复
C# 用 var q = list.GroupBy(x => x.claNo).Select(x => new { key = x.Key, name = string.Join(",", x.Select(y => y.stuNo)) }).ToList(); sql 用 for xml path。这个自己查一下就好了。很方便
xuzuning 2017-05-19
  • 打赏
  • 举报
回复
        static void Main(string[] args)
{
var a = new List<departModel>()
{
new departModel(){claNo = 1, stuNo = 1},
new departModel(){claNo = 1, stuNo = 2},
new departModel(){claNo = 1, stuNo = 3}
};

//查询
var query =
from p in a
group p by p.claNo;

foreach (var item in query)
{
Console.WriteLine("{0} => {1}", item.Key, string.Join(",", item.Select(x=>x.stuNo)));
}

//方法
var q =
a
.GroupBy(p => p.claNo);

foreach (var item in q)
{
Console.WriteLine("{0} => {1}", item.Key, string.Join(",", item.Select(x => x.stuNo)));
}

}
xuggzu 2017-05-19
  • 打赏
  • 举报
回复
判读clano,往stuno的list里塞不行吗?

110,535

社区成员

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

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

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