【发布时间】:2021-09-18 22:51:44
【问题描述】:
我有一个简单的 RegistrationCountByMonth 表
public int RegistrationCountByMonthId { get; set; }
public short? Year { get; set; }
public byte? Month { get; set; }
public int? NumberOfUsers { get; set; }
public virtual Month MonthNavigation { get; set; }
还有一个简单的 CleanByMonth 类型。我应该返回哪个
public short? Year { get; set; }
public byte? Month { get; set; }
public int? NumberOfUsers { get; set; }
为什么我不能这样做:
List<CleanByMonth> cleanMonths = crudeInfoByMonth.Value.Select(x => new { x.Year , x.Month, x.NumberOfUsers}).ToList();
crudeInfoByMonth 是 RegistrationCountByMonth 类型。
我得到了这个错误...
Cannot convert source type '
System.Collections.Generic.List<{
System.Nullable<short> Year,
System.Nullable<byte> Month,
System.Nullable<int> NumberOfUsers}>'
to target type 'System.Collections.Generic.List<Server.ViewModel.CleanByMonth>'
【问题讨论】:
-
您需要执行以下操作:
List<CleanByMonth> cleanMonths = crudeInfoByMonth.Value.Select(x => new CleanByMonth { x.Year , x.Month, x.NumberOfUsers}).ToList();(给予或接受)。你不能只选择一个新的匿名对象,你必须创建新的CkeanByMonth实例 -
@Flydog57 感谢您的帮助。但它仍然不起作用。我做了这样的事情:
List<CleanByMonth> cleanMonths = crudeInfoByMonth.Value .Select(x => new CleanByMonth {x.Year , x.Month, x.NumberOfUsers}).ToList();在每个x.field上都说无法解析符号“添加”