【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.IEnumerable”
【发布时间】:2013-05-14 18:45:00
【问题描述】:

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

这是我的控制器

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

【问题讨论】:

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


【解决方案1】:

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

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

喜欢:

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

然后修改你的函数返回IEnumerable&lt;MyClass&gt;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;

}

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

【讨论】:

  • 您需要将申请者数据的类型更改为 MyClass 并将分配重新放入。代码的全部目的是避免查询已经在缓存中!
  • 我认为先生这不起作用,因为申请人数据是我的容器
  • @BobVale,是的。感谢您指出这一点,我更关注错误,然后是一般方法,但再次感谢
  • @EnriqueGil,我不确定您的设计,但您可以将 IEnumerable&lt;MyClass&gt; 放入缓存中,但我想您知道收到错误的原因,您正在选择匿名对象和试图返回与代码中不同的类型。
【解决方案2】:

您将申请人数据定义为IEnumerable&lt;APPLICANT&gt;,但申请人列表的返回类型为IEnumerable&lt;{Anonymous Type}&gt;

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

【讨论】:

    【解决方案3】:

    你可以设置你的返回类型

    IQueryable
    

    然后返回

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

    喜欢

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

    会解决你的问题。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2011-05-14
          • 2013-05-15
          • 1970-01-01
          • 1970-01-01
          • 2015-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-12
          相关资源
          最近更新 更多