【问题标题】:Cannot implicitly convert type 'List<AnonymousType#1>' to 'IEnumerable<Models.APPLICANT>'无法将类型“List<AnonymousType#1>”隐式转换为“IEnumerable<Models.APPLICANT>”
【发布时间】:2013-05-17 06:24:48
【问题描述】:

尝试左连接 2 个表

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


        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
                                            {
                                                Profiles = a,
                                                APPLICANTs= j,


                                    }).Take(1000);


            applicantdata = applicantList.AsQueryable().OrderBy(v => v.APPLICANT_ID).ToList(); 
            if (applicantdata.Any())
            {
                Cache.Set("applicants", applicantdata, 30);
            }
        }
        return applicantdata;

    }

但问题是我在最后一行有错误

applicantdata = applicantList.AsQueryable().OrderBy(v => v.APPLICANT_ID).ToList();

错误提示

错误 1 ​​无法将类型“System.Collections.Generic.List&lt;AnonymousType#1&gt;”隐式转换为“System.Collections.Generic.IEnumerable&lt;Models.APPLICANT&gt;”。存在显式转换(您是否缺少演员表)

这是我的申请类别

[DataContract(IsReference = true)]
public partial class APPLICANT
{
    [DataMember]
    public int APPLICANT_ID { get; set; }
    [DataMember]
    public string APPLICANT_LastName { get; set; }
    [DataMember]
    public string APPLICANT_FirstName { get; set; }
    [DataMember]
    public string APPLICANT_MiddleName { get; set; }
    [DataMember]
    public string APPLICANT_Address { get; set; }
    [DataMember]
    public string APPLICANT_City { get; set; }
    [DataMember]
    public string APPLICANT_Phone { get; set; }
    [DataMember]
    public string APPLICANT_Email { get; set; }
    [DataMember]
    public Nullable<int> Profile_id { get; set; }

【问题讨论】:

  • 您的泛型类型参数没有显示在您的问题标题和引用的错误消息中,这使得问题远未明确。我对它们进行了编辑以使其更加可见
  • 能否添加“APPLICANT”类?您的问题是,当您编写“select new {”时,它会创建匿名类型,然后您尝试将其转换为“APPLICANT” int 这一行: ();.如果您提供所需的课程,我认为我可以帮助您

标签: c# linq entity-framework


【解决方案1】:

问题在于集合的类型。

 IEnumerable<APPLICANT> applicantdata ...

不等于你从这个表达式得到的类型:

 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  //<-- this is what dicides the type of the applicationList
             {
                 Profiles = a,
                 APPLICANTs= j,
       }).Take(1000);

这意味着你不能这样做:

 applicantdata = applicantList...

我认为你需要这样做:

applicantdata = applicantList
                   .SelectMany(c => c.APPLICANTs) //this line added
                   .AsQueryable().OrderBy(v => v.APPLICANT_ID).ToList();

更新

如果您将我的“解决方案”与SelectMany 一起使用,您应该问自己——“我在创建applicationList 时是否提取了正确的数据..?”

也许这样更好..:

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 j //<--  this is APPLICANTs type
       }).Take(1000);

【讨论】:

  • 添加该行之后。 .弹出新错误 方法 'System.Linq.Queryable.SelectMany(System.Linq.IQueryable, System.Linq.Expressions.Expression>>)' 不能从用法中推断出来。尝试明确指定类型参数。
  • 如果没有APPLICANTs 上的元数据,很难说问题出在哪里。可以发一下吗
  • @EnriqueGil 课程看起来不错。问题是什么。如果您仍然使用 SelectMany 解决方案,请尝试添加 Type praram:SelectMany&lt;APPLICANTs&gt;(c =&gt; c.APPLICANTs)..
  • 在尝试您的代码后,出现此错误 Application.Models.APPLICANT' 不包含“APPLICANTs”的定义,并且没有扩展方法“APPLICANTs”接受“Applicant.Models.APPLICANT”类型的第一个参数可以找到(您是否缺少 using 指令或程序集引用?)
  • 这一行的错误可能是因为 APPLICANTs 是我的 DBSet 。 .我现在应该用什么?申请者数据 = 申请者列表 .SelectMany(c => c.APPLICANTs) //这一行添加了 .AsQueryable().OrderBy(v => v.APPLICANT_ID).ToList();
【解决方案2】:

我认为您的问题不是从列表转换为 IEnumerable。相反,问题在于您无法将匿名对象的 LIST 转换为 APPLICANT 的 IEnumerable。

您应该修改您的选择语句,以便选择一个 APPLICANT。

【讨论】:

    猜你喜欢
    • 2013-07-24
    • 2013-05-14
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多