C# 无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.IEnumerable”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16645626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:32:49  来源:igfitidea点击:

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable

c#asp.netasp.net-mvclinqentity-framework

提问by Enrique Gil

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?)

无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.IEnumerable<Applicant.Models.APPLICANT>”。存在显式转换(您是否缺少演员表?)

回答by Habib

applicantdatais IEnumerable<APPLICANT>, in your select statement you are selecting anonymous type objectusing newkeyword, that is why you can't convert it to IEnumerable<APPLICANT>.

applicantdataIEnumerable<APPLICANT>,在您的 select 语句中,您使用关键字选择匿名类型对象new,这就是为什么您不能将其转换为IEnumerable<APPLICANT>.

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

您必须使用您的属性创建一个临时类,如 select 语句和IEnumerable该类的返回。

Like:

喜欢:

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

Then modify your function to return IEnumerable<MyClass>like

然后修改你的函数返回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;

}

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

您不能投影到APPLICANT,因为这似乎是通过实体框架生成的类。

回答by Bob Vale

You are defining applicant data to be a IEnumerable<APPLICANT>however the return type of applicant list is an IEnumerable<{Anonymous Type}>

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

You need some function to convert from your anonymous value to an Applicant if you want to use it in this fashion.

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

回答by ???

You Can set your return type

您可以设置您的退货类型

IQueryable

Then return

然后返回

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

Like

喜欢

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

}

Will solve your problem.

会解决你的问题。

回答by PEO

I think this is not an efficient way to implement because when you call .ToList it execute the query and grab the data to the memory and after that you again convert to .AsQueryable(). Imagine if you have thousand records and how much it cost. If I am wrong please correct me. Cheers

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

回答by DR.

Instead of returning IEnumerable rather return IEnumerable and then when you do the query call .ToLost() on it.

而不是返回 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;

    }