1

现在我正在尝试右加入一个异常弹出。

这是我的控制器

   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<AnonymousType#1>”隐式转换为“System.Collections.Generic.IEnumerable<Applicant.Models.APPLICANT>”。存在显式转换(您是否缺少演员表?)

4

5 回答 5

2

applicantdata也就是说IEnumerable<APPLICANT>,在您的 select 语句中,您正在使用关键字选择匿名类型对象new,这就是您无法将其转换为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,因为这似乎是通过实体框架生成的类。

于 2013-05-20T08:44:51.863 回答
0

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

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

于 2013-05-20T08:46:13.873 回答
0

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

于 2014-04-23T21:26:15.920 回答
0

您可以设置您的返回类型

IQueryable

然后返回

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

喜欢

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

}

会解决你的问题。

于 2013-09-21T05:29:31.817 回答
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;

    }
于 2014-07-19T09:26:42.130 回答