我有一个项目列表:List<Item>
我想将其转换为Items + quarters -> list<ItemModel>
项目还包含另一个列表,即ItemDetails。
如何将Item对象复制到另一个Item对象并使用LinQ从CreatedDate检索四分之一?
我还添加了Quarter结构,将Date转换为季度。但我无法将list<Item>
转换为list<ItemModel>
class Item{
public int Id { get; set; }
public DateTime CreatedDate { get; set; }
public string Name { get; set; }
public List<NameValuePair> ItemDetails { get; set; }
}
public struct Quarter
{
public Quarter(int year, int quarterIndex)
{
Year = year;
QuarterIndex = quarterIndex;
}
public static Quarter FromDate(DateTime date)
{
return new Quarter(date.Year, 1 + (date.Month - 1) / 3);
}
public int Year;
public int QuarterIndex;
}
public class ItemModel
{
public Item Item { get; set; }
public Quarter Quarter { get; set; }
}
你能帮帮我吗?
答案 0 :(得分:1)
您无法将List<Item>
转换为List<ItemModel>
,但您可以创建新列表:
List<Item> itemList = ...;
List<ItemModel> itemModelList = itemList
.Select(item => new ItemModel { Item = item, Quarter = Quarter.FromDate(item.CreatedDate)})
.ToList();