无法将类型'System.Collections.Generic.List <anonymoustype#1>'隐式转换为'System.Collections.Generic.IEnumerable </anonymoustype#1>

时间:2013-05-20 08:41:02

标签: c# asp.net asp.net-mvc linq entity-framework

现在我正在尝试正确加入一个异常突然出现。

这是我的控制器

   public IEnumerable<APPLICANT> GetApplicant()
    {
        IEnumerable<APPLICANT> applicantdata = Cache.Get("applicants") as IEnumerable<APPLICANT>;
        IEnumerable<Profile> profiledata = Cache.Get("profiles") as IEnumerable<Profile>;


        if (applicantdata == null)
        {

            var applicantList = (from a in context.Profiles 
                                 join app in context.APPLICANTs
                                 on a.PROFILE_ID equals app.Profile_id into joined
                                 from j in joined.DefaultIfEmpty()
                                 select new
                                            {
                                               APPLICANT = j, 
                                               Profile = a,
                                            }).Take(1000).AsEnumerable();

                   applicantdata = applicantList.AsEnumerable().ToList();


            if (applicantdata.Any())
            {
                Cache.Set("applicants", applicantdata, 30);
            }
        }
        return applicantdata;

    }

这是

上的错误
 applicantdata = applicantList.AsEnumerable().ToList();
  

无法隐式转换类型'System.Collections.Generic.List&lt; AnonymousType#1&gt;'至   'System.Collections.Generic.IEnumerable&LT; Applicant.Models.APPLICANT&GT;'。一个明确的   存在转换(你错过了演员吗?)

5 个答案:

答案 0 :(得分:1)

applicantdataIEnumerable<APPLICANT>,在您的选择语句中,您使用new关键字选择anonymous type object,这就是您无法将其转换为IEnumerable<APPLICANT>的原因。

您必须使用属性创建一个临时类,如同在select语句中一样,并返回该类的IEnumerable

像:

public class MyClass
{
  public APPLICANT applicant {get;set;}
  public Profile porfile {get;set;}
}

然后修改您的函数以返回IEnumerable<MyClass>,如

public IEnumerable<MyClass> GetApplicant()
{
    IEnumerable<MyClass> applicantdata = Cache.Get("applicants") as IEnumerable<MyClass>;
    IEnumerable<Profile> profiledata = Cache.Get("profiles") as IEnumerable<Profile>;

    IEnumerable<MyClass> applicantList;
    if (applicantdata == null)
    {

        applicantList = (from a in context.Profiles 
                             join app in context.APPLICANTs
                             on a.PROFILE_ID equals app.Profile_id into joined
                             from j in joined.DefaultIfEmpty()
                             select new MyClass //Change here
                                        {
                                           APPLICANT = j, 
                                           Profile = a,
                                        }).Take(1000);

               applicantdata = applicantList.AsEnumerable();



        if (applicantdata != null && applicantdata.Any())
        {
            Cache.Set("applicants", applicantdata, 30);
        }
    }
    return applicantdata;

}

您无法投射到APPLICANT,因为它似乎是通过实体框架生成的类。

答案 1 :(得分:0)

您正在将申请人数据定义为IEnumerable<APPLICANT>,但申请人列表的返回类型为IEnumerable<{Anonymous Type}>

如果您想以这种方式使用它,您需要一些功能来将您的匿名值转换为申请人。

答案 2 :(得分:0)

您可以设置返回类型

IQueryable

然后返回

applicantList.AsEnumerable().ToList().AsIQueryable();

public IEnumerable<APPLICANT> GetApplicant()
{
  applicantdata = applicantList.AsEnumerable().ToList().AsQueryable();

}

将解决您的问题。

答案 3 :(得分:0)

我认为这不是一种有效的实现方式,因为当你调用.ToList时它会执行查询并将数据存储到内存中,然后再次转换为.AsQueryable()。想象一下,如果你有千条记录和多少钱。 如果我错了,请纠正我。干杯

答案 4 :(得分:0)

而不是返回IEnumerable而是返回IEnumerable然后当你执行查询时调用.ToLost()就可以了。

public IEnumerable GetApplicant()
    {
        IEnumerable<APPLICANT> applicantdata = Cache.Get("applicants") as IEnumerable<APPLICANT>;
        IEnumerable<Profile> profiledata = Cache.Get("profiles") as IEnumerable<Profile>;


        if (applicantdata == null)
        {

            var applicantList = (from a in context.Profiles 
                                 join app in context.APPLICANTs
                                 on a.PROFILE_ID equals app.Profile_id into joined
                                 from j in joined.DefaultIfEmpty()
                                 select new
                                            {
                                               APPLICANT = j, 
                                               Profile = a,
                                            }).Take(1000); //AsEnumerable();

                   applicantdata = applicantList.ToList(); //applicantList.AsEnumerable();


            if (applicantdata.Any())
            {
                Cache.Set("applicants", applicantdata, 30);
            }
        }
        return applicantList.ToList(); //applicantdata;

    }
相关问题