无法隐式转换类型'System.Collections.Generic.List< AnonymousType#1>'为“System.Collections.Generic.IEnumerable [英] Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable

查看:1212
本文介绍了无法隐式转换类型'System.Collections.Generic.List< AnonymousType#1>'为“System.Collections.Generic.IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我尝试了右连接弹出一个例外。

这是我的控制器

 公开的IEnumerable<申请人GT; GetApplicant()
    {
        IEnumerable的<申请人GT; applicantdata = Cache.Get(申请人)为IEnumerable<申请人取代;
        IEnumerable的<&档案GT; profiledata = Cache.Get(配置文件)为IEnumerable<简介取代;
        如果(applicantdata == NULL)
        {            在context.Profiles变种applicantList =(从
                                 加入context.APPLICANTs应用
                                 在a.PROFILE_ID等于app.Profile_id成加盟
                                 从joined.DefaultIfEmptyĴ()
                                 新选择
                                            {
                                               申请人= j的,
                                               外形= A,
                                            })采取(1000).AsEnumerable();                   applicantdata = applicantList.AsEnumerable()了ToList()。
            如果(applicantdata.Any())
            {
                Cache.Set(申请人,applicantdata,30);
            }
        }
        返回applicantdata;    }

这是错误

  applicantdata = applicantList.AsEnumerable()了ToList()。


  

无法隐式转换类型'System.Collections.Generic.List< AnonymousType#1>'至
  System.Collections.Generic.IEnumerable< Applicant.Models.APPLICANT>'。明确
  转换存在(是否缺少强制转换?)



解决方案

applicantdata 的IEnumerable&LT;申请人GT; ,在你的SELECT语句您选择href=\"http://msdn.microsoft.com/en-us/library/vstudio/bb397696.aspx\" rel=\"nofollow\">匿名类型的对象使用<$ 关键字,这就是为什么你不能把它转换为的IEnumerable&LT;申请人GT;

您必须创建您的属性temporay类SELECT语句,并返回该类的的IEnumerable

我爱:

 公共类MyClass的
{
  公共申请人申请获取{;设置;}
  公开信息porfile {获取;设置;}
}

然后修改函数返回的IEnumerable&LT; MyClass的&GT;

 公开的IEnumerable&LT; MyClass的&GT; GetApplicant()
{
    IEnumerable的&LT; MyClass的&GT; applicantdata = Cache.Get(申请人)为IEnumerable&LT; MyClass的取代;
    IEnumerable的&LT;&档案GT; profiledata = Cache.Get(配置文件)为IEnumerable&LT;简介取代;    IEnumerable的&LT; MyClass的&GT; applicantList;
    如果(applicantdata == NULL)
    {        applicantList =(来自于context.Profiles
                             加入context.APPLICANTs应用
                             在a.PROFILE_ID等于app.Profile_id成加盟
                             从joined.DefaultIfEmptyĴ()
                             选择新MyClass的//改变这里
                                        {
                                           申请人= j的,
                                           外形= A,
                                        })以(1000);               applicantdata = applicantList.AsEnumerable();        如果(applicantdata = NULL&放大器;!&安培; applicantdata.Any())
        {
            Cache.Set(申请人,applicantdata,30);
        }
    }
    返回applicantdata;}

您不能投射到申请人,因为这似乎是通过实体框架生成的类。

Now that im trying the Right Join An Exception Popped up.

This is my Controller

   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;

    }

This is the error on

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

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<Applicant.Models.APPLICANT>'. An explicit conversion exists (are you missing a cast?)

解决方案

applicantdata is IEnumerable<APPLICANT>, in your select statement you are selecting anonymous type object using new keyword, that is why you can't convert it to IEnumerable<APPLICANT>.

You have to create a temporay class with your properties as in select statement and return IEnumerable of that class.

Like:

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

Then modify your function to return IEnumerable<MyClass> like

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;

}

You can't project to APPLICANT, since that appears to be a class generated through entity framework.

这篇关于无法隐式转换类型'System.Collections.Generic.List&LT; AnonymousType#1&GT;'为“System.Collections.Generic.IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆