求LINQ查询课程表的语句
课程表:
A 1 A 2 A 3 B 4
A 1 B 2 C 3 C 4
A 1 A 2 C 3 B 4
B 1
B 1
C 1
A/B/C代表课程名字,1、2、3、4代表星期几,类定义如下:
C# code
public class schedule
{
public string name { get; set; }
public int weekDay { get; set; }
}
请问怎样写以下这种情况的LINQ语句:
1、查询星期一、二同时有课的课程,按这个课程表查询结果应该是:A B
2、查询星期一、二、三同时有课的课程,按这个课程表查询结果应该是:A
3、查询任意天数星期同时有课的课程。任意天数用List<int>表示。
其实问题1、2、3可以用同一个LINQ语句来实现,请高手写出详细可执行代码,谢谢!
------解决方案--------------------
List<int> days = new List<int>();
days.Add(1);
days.Add(2);
days.Add(3);
=>
List<int> days = Enumerable.Range(1, 3).ToList();
var query = from x in allSche
group x by x.name into g
where days.All(z => g.Select(y => y.weekDay).Contains(z))
select g.Where(x => days.Contains(x.weekDay));
string name = "";
foreach(var q in query)
{
foreach (var qq in q)
{
name += qq.name + " " + qq.weekDay.ToString() + Environment.NewLine;
}
}
this.rtb_searchContent.Text = name;
=>
var query = (from x in allSche
group x by x.name into g
where days.All(z => g.Select(y => y.weekDay).Contains(z))
select g.Where(x => days.Contains(x.weekDay))).SelectManay(x => x);
this.rtb_searchContent.Text = string.Join("\r\n", query.Select(x => string.Format("{0} {1}", x.name, x.weekDay)).ToArray());