[英]Linq: add objects from one list to another with an other type, (MVC, C#)
I have a "FindItemsResults <Appointment>
list" which contains appointment objects. 我有一个“ FindItemsResults <Appointment>
列表”,其中包含约会对象。
I want to select "Start" property value and of all appointment objects from this List and save these values in an other list of type DateTime ... List<DateTime> avaliableBookingDays
我想从此列表中选择“开始”属性值以及所有约会对象,并将这些值保存在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);
My LINQ exepression fails: 我的LINQ表达式失败:
IEnumerable<DateTime> res = from x in appointments
select new DateTime { (x.Start};
I get error, like that: 我收到这样的错误:
Cannot inittialize type "DateTime" with a collection inittializer because it do not implement "System.Collections.IEnumerable" 无法使用集合初始化器初始化类型“ DateTime”,因为它没有实现“ System.Collections.IEnumerable”
The above-mentioned LINQ must do this: 上述LINQ必须做到这一点:
foreach (var appObj in appointments)
{
avaliableBookingDays.Add(appObj.Start);
}
If you want to project items in sequence to some other type, use Enumerable.Select
: 如果Enumerable.Select
顺序将项目投影为其他类型,请使用Enumerable.Select
:
var avaliableBookingDays = appointments.Select(a => a.Start).ToList();
Note that there is no equivalent of ToList()
operator in query syntax. 请注意,查询语法中没有等效的ToList()
运算符。 So the best you can do is get IEnumerable<DateTime>
result and convert it to list later: 因此,最好的办法是获取IEnumerable<DateTime>
结果并将其转换为以后的列表:
IEnumerable<DateTime> res = from x in appointments
select x.Start;
var avaliableBookingDays = res.ToList();
Also keep in mind - DateTime
is a value type . 另外请记住DateTime
是一个值类型 。 You don't need to create new instance of this type to get a copy of appointment start time - copy will be created automatically. 您无需创建此类型的新实例即可获取约会开始时间的副本-副本将自动创建。
Your code is not even going to compile 您的代码甚至都不会编译
IEnumerable<DateTime> res = from x in appointments
select new DateTime { (x.Start};
because it has syntax errors ! 因为它有语法错误! In your select part, you are projecting to a new DateTime object and you have just an opening (
, which does not make any sense! 在您选择的部分中,您将投影到一个新的DateTime对象,并且只有一个开口(
,这没有任何意义!
I think you were getting the mentioned error when you had the closing bracket as well like 我认为当您使用右括号时,您会遇到上述错误
IEnumerable<DateTime> res = from x in appointments
select new DateTime { (x.Start)};
Here your code is trying (assume it was valid) to project the items from appointments
collection to another collection of DateTime
with some invalid syntax ! 在这里,您的代码正在尝试(假设它是有效的)使用一些无效的语法将项目从appointments
集合投影到DateTime
另一个集合! There is no need to use the new
keyword here as you are not trying to create an object of a view model class/annonymous object since all you need is the Start property value. 由于您不需要创建视图模型类/匿名对象的对象,因此无需在此处使用new
关键字,因为您所需的只是Start属性值。
Solution 解
Since Start
is the DateTime
type property, you want to select only that. 由于Start
是DateTime
类型的属性,因此您只需要选择它。
List<DateTime> avaliableBookingDays = (from x in appointments
select x.Start).ToList();
Or 要么
List<DateTime> avaliableBookingDays = appointments.Select(a=>a.Start).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.