无法将类型'List <anonymoustype#1>'隐式转换为'IEnumerable <models.applicant>'</models.applicant> </anonymoustype#1>

时间:2013-05-17 06:24:49

标签: c# linq entity-framework

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

这是我的申请人类

[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; }

2 个答案:

答案 0 :(得分:4)

这是问题的集合类型。

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

答案 1 :(得分:1)

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

您应该修改select语句,以便选择APPLICANT。