无法将LINQ查询的结果强制转换为IEnumerable <myclasstype>

时间:2017-11-10 18:15:22

标签: entity-framework linq

  

无法投射类型的对象   “System.Data.Entity.Infrastructure.DbQuery`1并[d&GT; f__AnonymousType1`8 [System.Int32,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_CONSULTANT_LIST]'。

这是我的CUSTOM_CONSULTANT_LIST类:

  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();
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的查询返回匿名类型的枚举,如错误消息所示(这是“String symptom = (String)dataSnapshot.child("disease").getValue(); // the return type is an object // but internally the appropriate native type will be returned ”位;匿名类型在某种意义上确实有名称,但它们由不可言状的Lovecraftian线路噪声组成和chthonic jabberwocky):

<>f__AnonymousType18[System.Int32,...

试试这个:

select new
{
    c.ID_CONSULTANT, c.NME_FIRST, ...