需要有关开发 LINQ Lambda 表达式以按计划进行筛选的帮助
我很难找到 lambda 表达式来调用列表以正确过滤对象结构。 我希望这里有人可以提供帮助。 我使用的是.NET 3.5和LINQ,并且对象域是从Linq设置为SQL DBML。 我特别想使用lambda 表达式。
对象结构是人们具有设置的结构,其中一个设置是计划名称,它对应于具有开始日期和结束日期的另一个计划类。
public class MyPerson
{
public int ID { get; set; }
public List<MySetting> Settings { get; set; }
}
public class MySetting
{
public int ID { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
public class MySchedule
{
public string ID { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
计划有开始日期和结束日期,并且人员的状态可以是“活动”或“已计划”。
List<MySchedule> schedules = new List<MySchedule>();
MySchedule scheduleA = new MySchedule { ID = "ScheduleA", StartDate = Convert.ToDateTime("1/1/2008"), EndDate = Convert.ToDateTime("12/31/2008") };
MySchedule scheduleB = new MySchedule { ID = "ScheduleB", StartDate = Convert.ToDateTime("1/1/2009"), EndDate = Convert.ToDateTime("12/31/2009") };
schedules.Add(scheduleA);
schedules.Add(scheduleB);
List<MySetting> settingsJill = new List<MySetting>();
MySetting settingFirstName = new MySetting { ID = 1, Name = "FirstName", Value = "Jill" };
MySetting settingScheduleName = new MySetting { ID = 2, Name = "ScheduleName", Value = "ScheduleB" };
MySetting settingState = new MySetting { ID = 3, Name = "State", Value = "Scheduled" }; // Jill uses ScheduleB
settingsJill.Add(settingFirstName);
settingsJill.Add(settingScheduleName);
settingsJill.Add(settingState);
List<MySetting> settingsBill = new List<MySetting>();
settingFirstName = new MySetting { ID = 1, Name = "FirstName", Value = "Bill" };
settingScheduleName = new MySetting { ID = 2, Name = "ScheduleName", Value = "ScheduleA" };
settingState = new MySetting { ID = 3, Name = "State", Value = "Scheduled" }; // Bill is Scheduled last year
settingsBill.Add(settingFirstName);
settingsBill.Add(settingScheduleName);
settingsBill.Add(settingState);
List<MySetting> settingsJane = new List<MySetting>();
settingFirstName = new MySetting { ID = 1, Name = "FirstName", Value = "Jane" };
settingScheduleName = new MySetting { ID = 2, Name = "ScheduleName", Value = "ScheduleA" };
settingState = new MySetting { ID = 3, Name = "State", Value = "Active" }; // Jane is Active
settingsJane.Add(settingFirstName);
settingsJane.Add(settingScheduleName);
settingsJane.Add(settingState);
List<MyPerson> persons = new List<MyPerson>();
MyPerson Jane = new MyPerson { ID = 1, Settings = settingsJane };
MyPerson Jill = new MyPerson { ID = 2, Settings = settingsJill };
persons.Add(Jane);
persons.Add(Jill);
persons.Add(Bill);
我想通过创建一个 lambda 表达式来过滤人员列表,该表达式将返回 State = "Active" 的人员或 ScheduleName 包含开始日期和结束日期之间的当前日期。 换句话说,Jill 应该出现在筛选列表中,因为她已安排,并且她使用 ScheduleB,而 ScheduleB 是 2009 年的,而今天是 6/17/2009。
我首先感谢
List<MyPerson> filter = persons.Select ( // and this is where I get stuck.
您的帮助。
I am having a hard time finding the lambda expression to call on a list to correctly filter an object structure. I was hoping someone here could help out. I am using .NET 3.5, and LINQ and the object domain is set up from Linq to SQL DBML. I specifically want to use lambda expressions.
The object structure is one where persons have settings, and one setting is a Schedule name, which corresponds to another schedule class with a StartDate and End Date.
public class MyPerson
{
public int ID { get; set; }
public List<MySetting> Settings { get; set; }
}
public class MySetting
{
public int ID { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
public class MySchedule
{
public string ID { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
Schedules have Start and End Dates, and a person's State can be Active or Scheduled.
List<MySchedule> schedules = new List<MySchedule>();
MySchedule scheduleA = new MySchedule { ID = "ScheduleA", StartDate = Convert.ToDateTime("1/1/2008"), EndDate = Convert.ToDateTime("12/31/2008") };
MySchedule scheduleB = new MySchedule { ID = "ScheduleB", StartDate = Convert.ToDateTime("1/1/2009"), EndDate = Convert.ToDateTime("12/31/2009") };
schedules.Add(scheduleA);
schedules.Add(scheduleB);
List<MySetting> settingsJill = new List<MySetting>();
MySetting settingFirstName = new MySetting { ID = 1, Name = "FirstName", Value = "Jill" };
MySetting settingScheduleName = new MySetting { ID = 2, Name = "ScheduleName", Value = "ScheduleB" };
MySetting settingState = new MySetting { ID = 3, Name = "State", Value = "Scheduled" }; // Jill uses ScheduleB
settingsJill.Add(settingFirstName);
settingsJill.Add(settingScheduleName);
settingsJill.Add(settingState);
List<MySetting> settingsBill = new List<MySetting>();
settingFirstName = new MySetting { ID = 1, Name = "FirstName", Value = "Bill" };
settingScheduleName = new MySetting { ID = 2, Name = "ScheduleName", Value = "ScheduleA" };
settingState = new MySetting { ID = 3, Name = "State", Value = "Scheduled" }; // Bill is Scheduled last year
settingsBill.Add(settingFirstName);
settingsBill.Add(settingScheduleName);
settingsBill.Add(settingState);
List<MySetting> settingsJane = new List<MySetting>();
settingFirstName = new MySetting { ID = 1, Name = "FirstName", Value = "Jane" };
settingScheduleName = new MySetting { ID = 2, Name = "ScheduleName", Value = "ScheduleA" };
settingState = new MySetting { ID = 3, Name = "State", Value = "Active" }; // Jane is Active
settingsJane.Add(settingFirstName);
settingsJane.Add(settingScheduleName);
settingsJane.Add(settingState);
List<MyPerson> persons = new List<MyPerson>();
MyPerson Jane = new MyPerson { ID = 1, Settings = settingsJane };
MyPerson Jill = new MyPerson { ID = 2, Settings = settingsJill };
persons.Add(Jane);
persons.Add(Jill);
persons.Add(Bill);
I want to filter the persons List by creating a lambda expression that will return persons with State = "Active" OR with a ScheduleName containing a current Date between the Start and End Date. In other words, Jill should appear in the filtered list since she is Scheduled, and she uses ScheduleB, and ScheduleB is for 2009, and today is 6/17/2009.
I am starting with
List<MyPerson> filter = persons.Select ( // and this is where I get stuck.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要重新考虑如何存储信息。 .NET Collectionns 不是 SQL 表,您不应该像这样使用它们。 话虽这么说,这是您想要的查询:
想象一下,如果您有一个如下的数据结构:
You need to rethink how you are storing the information. .NET Collectionns aren't SQL tables, and you shouldn't use them like so. That being said, here's the query you wanted:
Imagine if you had a data structure like:
一个问题是您从
Select
开始。 这是用于投影的 - 你只是想过滤,所以你只需要Where
。另一个问题是您的
MyPerson
类型不包含ScheduleName
或State
属性。 确实不清楚这一切是如何结合在一起的。 如果您可以纠正您的示例,我相信我们可以提出过滤器。One problem is that you're starting with a
Select
. That's for projection - you're just trying to filter, so you only needWhere
.Another problem is that your
MyPerson
type doesn't include aScheduleName
orState
property. It's really not clear how it's all meant to hang together. If you could correct your example, I'm sure we can come up with the filter.