Entity framework 无法将LINQ查询的结果强制转换为IEnumerable
无法强制转换类型为的对象 'System.Data.Entity.Infrastructure.DbQuery`1[f_uAnonymousType1`8[System.Int32,System.String,System.String,System.String,System.String,System.String,System.String,System.Nullable`1[System.Boolean],System.Nullable`1[System.DateTime]] 键入“System.Collections.Generic.IEnumerable`1[ConsultantManagement.CustomBuffers.CUSTOM\u CONSULTANT\u LIST]” 这是我的定制顾问列表课程:Entity framework 无法将LINQ查询的结果强制转换为IEnumerable,entity-framework,linq,Entity Framework,Linq,无法强制转换类型为的对象 'System.Data.Entity.Infrastructure.DbQuery`1[f_uAnonymousType1`8[System.Int32,System.String,System.String,System.String,System.String,System.String,System.String,System.Nullable`1[System.Boolean],System.Nullable`1[System.DateTime]] 键入“Sy
public class CUSTOM_CONSULTANT_LIST
{
public int ID_CONSULTANT { get; set; }
public string NME_FIRST { get; set; }
public string NME_LAST { get; set; }
public string PHN_CELL { get; set; }
public string NME_PAY_TO { get; set; }
public string CDE_PAYMENT_METHOD { get; set; }
public Nullable<bool> IND_ACTIVE { get; set; }
// Added custom field to contain the MAX Invoice Date for the Consultant...
public Nullable<System.DateTime> LATEST_INVOICE_DATE { get; set; }
}
这是函数第一行失败的代码
public static IEnumerable<CUSTOM_CONSULTANT_LIST> Get_Consultant_List(ref ConsultantListViewModel vm)
{
// Retrieve the list of consultants with the latest (MAX) invoice date joined...
IEnumerable<CUSTOM_CONSULTANT_LIST> query = (IEnumerable<CUSTOM_CONSULTANT_LIST>)(from c in db.CCC_CONSULTANT
join i in db.CCC_INVOICE on c.ID_CONSULTANT equals i.ID_CONSULTANT
select new
{
c.ID_CONSULTANT, c.NME_FIRST, c.NME_LAST, c.PHN_CELL, c.NME_PAY_TO, c.CDE_PAYMENT_METHOD, c.IND_ACTIVE,
LATEST_INVOICE_DATE = c.CCC_CONSULTANT_ACTIVITY.Max((a) => a.DTM_ACTIVITY) });
return query.ToList();
}
我做错了什么?您的查询返回一个匿名类型的枚举,因为错误消息说这是f_uAnonymousType18[System.Int32,…bit;匿名类型在某种意义上确实有名称,但它们由无法形容的Lovecraftian line noise和Chtonic jabberwocky组成:
select new
{
c.ID_CONSULTANT, c.NME_FIRST, ...
试试这个:
select new CUSTOM_CONSULTANT_LIST
{
ID_CONSULTANT = c.ID_CONSULTANT,
NME_FIRST = c.NME_FIRST,
// ... other properties ...
LATEST_INVOICE_DATE = c.CCC_CONSULTANT_ACTIVITY.Max((a) => a.DTM_ACTIVITY)
}
是的-真实的事实-Cthulu生成anon classesEd的名称,非常感谢!