求一个LINQ语句
答案:5 悬赏:0 手机版
解决时间 2021-01-14 19:56
- 提问者网友:寂寞撕碎了回忆
- 2021-01-14 08:18
求一个LINQ语句
最佳答案
- 五星知识达人网友:罪歌
- 2021-01-14 09:39
int GetMenuKey(int index, IDictionary> s, IDictionary> t)
{
int menuKey = t.Where(x => x.Value.Contains(index)).FirstOrDefault().Key;
var tvalues= s.Where(x => x.Value.Contains(menuKey)).FirstOrDefault().Value;
var Alllist = t.Where(p => tvalues.Contains(p.Key)).SelectMany(p => p.Value).ToList();
return Alllist.IndexOf(index);
}
这样就可以了
{
int menuKey = t.Where(x => x.Value.Contains(index)).FirstOrDefault().Key;
var tvalues= s.Where(x => x.Value.Contains(menuKey)).FirstOrDefault().Value;
var Alllist = t.Where(p => tvalues.Contains(p.Key)).SelectMany(p => p.Value).ToList();
return Alllist.IndexOf(index);
}
这样就可以了
全部回答
- 1楼网友:老鼠爱大米
- 2021-01-14 12:46
public class Grade{ public int Id { get; set; } public List Students { get; set; }}public class Student{ public int Id { get; set; } public int GradeId { get; set; } public int Score { get; set; }}public static void Test(){ List mockGrades = new List() { new Grade() { Id = 1, Students = new List(){ new Student(){ Id=1, GradeId=1, Score=99}, new Student(){ Id=2, GradeId=1, Score=100}, new Student(){ Id=3, GradeId=1, Score=88}}}, new Grade() { Id = 2, Students = new List(){ new Student(){ Id=4, GradeId=2, Score=21}, new Student(){ Id=5, GradeId=2, Score=35}, new Student(){ Id=6, GradeId=2, Score=24}}}, new Grade() { Id = 3, Students = new List(){ new Student(){ Id=7, GradeId=3, Score=97}, new Student(){ Id=8, GradeId=3, Score=71}, new Student(){ Id=9, GradeId=3, Score=99}}}, new Grade() { Id = 4, Students = new List(){ new Student(){ Id=10, GradeId=4, Score=90}, new Student(){ Id=11, GradeId=4, Score=69}, new Student(){ Id=12, GradeId=4, Score=32}}}, new Grade() { Id = 5, Students = new List(){ new Student(){ Id=13, GradeId=5, Score=37}, new Student(){ Id=14, GradeId=5, Score=84}, new Student(){ Id=15, GradeId=5, Score=56}}} }; var result = from i in mockGrades
- 2楼网友:野味小生
- 2021-01-14 11:10
不明白什么意思,5 序号0, 23 序号7, 22 序号 0 ,19 序号0,32 序号7,为什么5,22,19的序号都是0,是不是问题弄错了?追问没弄错啊,三级目录的值是根据二级目录来分类的意思,根据二级目录的要求,共分为三个分支,然后求值在分支的序号追答int thirdval = Convert.ToInt32(Console.ReadLine());//给一个三级目录的值,
List secondval = secondlevel.Where(m => m.Value.Contains(thirdlevel.Where(n => n.Value.Contains(thirdval)).FirstOrDefault().Key)).FirstOrDefault().Value.ToList();
IDictionary> tmpthirdlevel = thirdlevel.Where(k => secondval.Contains(k.Key)).OrderBy(m => m.Key).ToDictionary(m => m.Key, m => m.Value);
List thirdvallist = new List();
foreach (var item in tmpthirdlevel)
{
foreach (int t in item.Value.OrderBy(m=>m))
{
thirdvallist.Add(t);
}
}
int index = thirdvallist.FindIndex(delegate(int t) { return t==thirdval; });
List
IDictionary
List
foreach (var item in tmpthirdlevel)
{
foreach (int t in item.Value.OrderBy(m=>m))
{
thirdvallist.Add(t);
}
}
int index = thirdvallist.FindIndex(delegate(int t) { return t==thirdval; });
- 3楼网友:千夜
- 2021-01-14 11:00
这个值对应的序号是怎么看的;
比如23为什么对应7 ,22对应0
能说明下吗追问没弄错啊,三级目录的值是根据二级目录来分类的意思,根据二级目录的要求,共分为三个分支,然后求值在分支的序号追答int value = 20;//三级目录的值
foreach (var item in thirdlevel.Values)
{
int index = item.Select((s, i) => new { s, i }).Where(p => p.s == value).Select(p => p.i).First();
}
比如23为什么对应7 ,22对应0
能说明下吗追问没弄错啊,三级目录的值是根据二级目录来分类的意思,根据二级目录的要求,共分为三个分支,然后求值在分支的序号追答int value = 20;//三级目录的值
foreach (var item in thirdlevel.Values)
{
int index = item.Select((s, i) => new { s, i }).Where(p => p.s == value).Select(p => p.i).First();
}
- 4楼网友:零点过十分
- 2021-01-14 10:14
我明白了!代码如下:
IDictionary templevel = new Dictionary();//键:三级目录值; 值:三级目录对应的Index
foreach (int key in secondlevel.Keys)//遍历二级目录的键
{
var q = from d in thirdlevel.Keys where secondlevel[key].Contains(d) select thirdlevel[d];
List listValue = new List();
foreach (var item in q)
{
listValue.AddRange(item);
}
//合并
templevel = templevel.Union(listValue.ToDictionary(v => v, v => listValue.IndexOf(v))).ToDictionary(a => a.Key, a => a.Value);
}
IDictionary
foreach (int key in secondlevel.Keys)//遍历二级目录的键
{
var q = from d in thirdlevel.Keys where secondlevel[key].Contains(d) select thirdlevel[d];
List
foreach (var item in q)
{
listValue.AddRange(item);
}
//合并
templevel = templevel.Union(listValue.ToDictionary(v => v, v => listValue.IndexOf(v))).ToDictionary(a => a.Key, a => a.Value);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯