Linq:将对象从一个列表添加到另一个具有另一种类型的列表,(MVC,C#)
Linq: add objects from one list to another with an other type, (MVC, C#)
我有一个 "FindItemsResults<Appointment>
list",其中包含约会对象。
我想 select "Start" 属性 值和此列表中所有约会对象的值,并将这些值保存在另一个 DateTime 类型的列表中... List<DateTime> avaliableBookingDays
List<DateTime> avaliableBookingDays = new List<DateTime>();
FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
IEnumerable<DateTime> res = from x in appointments
select new DateTime { (x.Start};
avaliableBookingDays.AddRange(res);
我的 LINQ 表达式失败:
IEnumerable<DateTime> res = from x in appointments
select new DateTime { (x.Start};
我收到这样的错误:
Cannot inittialize type "DateTime" with a collection inittializer
because it do not implement "System.Collections.IEnumerable"
上面提到的LINQ必须这样做:
foreach (var appObj in appointments)
{
avaliableBookingDays.Add(appObj.Start);
}
如果你想项目顺序项目到一些其他类型,使用Enumerable.Select
:
var avaliableBookingDays = appointments.Select(a => a.Start).ToList();
请注意,在查询语法中没有等效的 ToList()
运算符。所以你能做的最好的就是获得 IEnumerable<DateTime>
结果并稍后将其转换为列表:
IEnumerable<DateTime> res = from x in appointments
select x.Start;
var avaliableBookingDays = res.ToList();
另请记住 - DateTime
是 value type。您无需创建此类型的新实例即可获取约会开始时间的副本 - 副本将自动创建。
您的代码甚至无法编译
IEnumerable<DateTime> res = from x in appointments
select new DateTime { (x.Start};
因为它有语法错误!在你的 select 部分,你正在投影到一个新的 DateTime 对象,你只有一个空缺 (
,这没有任何意义!
我认为您在使用右括号时遇到了上述错误,例如
IEnumerable<DateTime> res = from x in appointments
select new DateTime { (x.Start)};
此处您的代码正在尝试(假设它是有效的)将 appointments
集合中的项目投影到 DateTime
的另一个集合,但语法无效!此处无需使用 new
关键字,因为您没有尝试创建视图模型 class/annonymous 对象的对象,因为您需要的只是 Start 属性 值。
解决方案
由于 Start
是 DateTime
类型 属性,您只想 select。
List<DateTime> avaliableBookingDays = (from x in appointments
select x.Start).ToList();
或
List<DateTime> avaliableBookingDays = appointments.Select(a=>a.Start).ToList();
我有一个 "FindItemsResults<Appointment>
list",其中包含约会对象。
我想 select "Start" 属性 值和此列表中所有约会对象的值,并将这些值保存在另一个 DateTime 类型的列表中... List<DateTime> avaliableBookingDays
List<DateTime> avaliableBookingDays = new List<DateTime>();
FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
IEnumerable<DateTime> res = from x in appointments
select new DateTime { (x.Start};
avaliableBookingDays.AddRange(res);
我的 LINQ 表达式失败:
IEnumerable<DateTime> res = from x in appointments
select new DateTime { (x.Start};
我收到这样的错误:
Cannot inittialize type "DateTime" with a collection inittializer because it do not implement "System.Collections.IEnumerable"
上面提到的LINQ必须这样做:
foreach (var appObj in appointments)
{
avaliableBookingDays.Add(appObj.Start);
}
如果你想项目顺序项目到一些其他类型,使用Enumerable.Select
:
var avaliableBookingDays = appointments.Select(a => a.Start).ToList();
请注意,在查询语法中没有等效的 ToList()
运算符。所以你能做的最好的就是获得 IEnumerable<DateTime>
结果并稍后将其转换为列表:
IEnumerable<DateTime> res = from x in appointments
select x.Start;
var avaliableBookingDays = res.ToList();
另请记住 - DateTime
是 value type。您无需创建此类型的新实例即可获取约会开始时间的副本 - 副本将自动创建。
您的代码甚至无法编译
IEnumerable<DateTime> res = from x in appointments
select new DateTime { (x.Start};
因为它有语法错误!在你的 select 部分,你正在投影到一个新的 DateTime 对象,你只有一个空缺 (
,这没有任何意义!
我认为您在使用右括号时遇到了上述错误,例如
IEnumerable<DateTime> res = from x in appointments
select new DateTime { (x.Start)};
此处您的代码正在尝试(假设它是有效的)将 appointments
集合中的项目投影到 DateTime
的另一个集合,但语法无效!此处无需使用 new
关键字,因为您没有尝试创建视图模型 class/annonymous 对象的对象,因为您需要的只是 Start 属性 值。
解决方案
由于 Start
是 DateTime
类型 属性,您只想 select。
List<DateTime> avaliableBookingDays = (from x in appointments
select x.Start).ToList();
或
List<DateTime> avaliableBookingDays = appointments.Select(a=>a.Start).ToList();