求LINQ查询课程表的语句

shally66 2012-08-02 10:11:10
课程表:
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代表星期几,类定义如下:

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语句来实现,请高手写出详细可执行代码,谢谢!
...全文
147 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
shally66 2012-08-02
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

List<int> days = new List<int>();
days.Add(1);
days.Add(2);
days.Add(3);
=>
List<int> days = Enumerable.Range(1, 3).ToList();




……
[/Quote]

高!受教了!
devmiao 2012-08-02
  • 打赏
  • 举报
回复
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());
devmiao 2012-08-02
  • 打赏
  • 举报
回复
List<schedule> allSche = new List<schedule>();
allSche.Add(new schedule { name = "A", weekDay = 1 });
allSche.Add(new schedule { name = "A", weekDay = 1 });
allSche.Add(new schedule { name = "A", weekDay = 1 });
allSche.Add(new schedule { name = "B", weekDay = 1 });
...
可以写成
List<schedule> allSche = new List<schedule>()
{
new schedule { name = "A", weekDay = 1 },
new schedule { name = "A", weekDay = 1 },
new schedule { name = "A", weekDay = 1 },
new schedule { name = "B", weekDay = 1 },
...
};
}
shally66 2012-08-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

var query = from x in 课程表
group x by x.name into g;
where new int[] { 1, 2 }.All(z => g.Select(y => y.weekDay).Contains(z))
select g.Where(x => new int[] { 1, 2 }.Contains(x.weekDay));

……
[/Quote]

List<schedule> allSche = new List<schedule>();
allSche.Add(new schedule { name = "A", weekDay = 1 });
allSche.Add(new schedule { name = "A", weekDay = 1 });
allSche.Add(new schedule { name = "A", weekDay = 1 });
allSche.Add(new schedule { name = "B", weekDay = 1 });
allSche.Add(new schedule { name = "B", weekDay = 1 });
allSche.Add(new schedule { name = "C", weekDay = 1 });
allSche.Add(new schedule { name = "A", weekDay = 2 });
allSche.Add(new schedule { name = "B", weekDay = 2 });
allSche.Add(new schedule { name = "A", weekDay = 2 });
allSche.Add(new schedule { name = "A", weekDay = 3 });
allSche.Add(new schedule { name = "C", weekDay = 3 });
allSche.Add(new schedule { name = "C", weekDay = 3 });
allSche.Add(new schedule { name = "B", weekDay = 4 });
allSche.Add(new schedule { name = "C", weekDay = 4 });
allSche.Add(new schedule { name = "B", weekDay = 4 });
List<int> days = new List<int>();
days.Add(1);
days.Add(2);
days.Add(3);
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;

十分正确,非常感谢!不知道是不是可以优化一下呢?
devmiao 2012-08-02
  • 打赏
  • 举报
回复
var query = from x in 课程表
group x by x.name into g;
where new int[] { 1, 2 }.All(z => g.Select(y => y.weekDay).Contains(z))
select g.Where(x => new int[] { 1, 2 }.Contains(x.weekDay));
shally66 2012-08-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

var query = from x in 课程表
group x by x.name into g;
where new int[] { 1, 2 }.All(z => g.Select(y => y.weekDay).Contains(z))
select g;
[/Quote]
还是不对,结果是:AAAAAABBBBB,理想结果应该是:AAAAABBB
devmiao 2012-08-02
  • 打赏
  • 举报
回复
var query = from x in 课程表
group x by x.name into g;
where new int[] { 1, 2 }.All(z => g.Select(y => y.weekDay).Contains(z))
select g;
shally66 2012-08-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

(1) var query = from x in 课程表
group x by x.name into g;
where g.Select(y => y.weekDay).All(y => new int[] { 1, 2 }.Contains(y))
select g;
(2)(3)……
[/Quote]
你试过吗?我试了结果为空。
shally66 2012-08-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

(1) var query = from x in 课程表
group x by x.name into g;
where g.Select(y => y.weekDay).All(y => new int[] { 1, 2 }.Contains(y))
select g;
(2)(3)……
[/Quote]

试一下。
天下如山 2012-08-02
  • 打赏
  • 举报
回复
个人感觉数据有点乱。
devmiao 2012-08-02
  • 打赏
  • 举报
回复
(1) var query = from x in 课程表
group x by x.name into g;
where g.Select(y => y.weekDay).All(y => new int[] { 1, 2 }.Contains(y))
select g;
(2)(3)类似

8,492

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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