简体   繁体   English

Linq:将对象从一个列表添加到另一种类型的列表(MVC,C#)

[英]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. 由于StartDateTime类型的属性,因此您只需要选择它。

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.

相关问题 C#LINQ如何从另一个列表中的一个列表中找到匹配项,并在另一个列表中检查属性 - C# LINQ How to find matching item from one list in another list and check for the property in the other list C#Linq从一个列表中添加元素 <custom> 与另一个比较它们并改变价值 - C# Linq add elements from one list<custom> to another comparing them and changing values C#将列表从一种类型转换为另一种类型 - C# Transform list from one type to another 将C#列表中的数据从一种类型转换为另一种类型。 - Converting Data in a C# List from one type to another. 将元素从一个列表添加到另一个C# - Add elements from one list to another C# C#如何将一个对象从一个列表添加到另一个 - C# how to add an object from one list to another 如何从LINQ查询返回匿名类型的对象(或对象列表)到C#中的UI(控制台应用程序)? - How to return an object (or a list of objects) of anonymous type from a LINQ query to the UI (Console App) in C#? Linq根据另一个列表的成员资格从一个列表中排除自定义对象(C#) - Linq to exclude a custom object from one List based on membership of another List (c#) 单击按钮MVC C#时逐个添加对象 - Add objects one by one when click button MVC C# 如何在C#MVC中的列表内添加对象列表 - How to add a list of objects inside a list in C# MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM