无法隐式转换类型“列表< AnonymousType#1>'为“IEnumerable的< Models.APPLICANT>' [英] Cannot implicitly convert type 'List<AnonymousType#1>' to 'IEnumerable<Models.APPLICANT>'
问题描述
试图左联接表2
公开的IEnumerable<申请人GT; GetApplicant()
{
IEnumerable的<申请人GT; applicantdata = Cache.Get(申请人)为IEnumerable<申请人取代;
如果(applicantdata == NULL)
{在context.Profiles
VAR applicantList =(从连接上下文中的应用在a.PROFILE_ID .APPLICANTs
等于app.Profile_id到joined.DefaultIfEmpty()
从j加入
选择新的
{
型材= A,$ b $ ; b申请人= j的,
})以(1000);
applicantdata = applicantList.AsQueryable()排序依据(V => v.APPLICANT_ID)。.ToList();
如果(applicantdata.Any())
{
Cache.Set(申请人,applicantdata,30);
}
}
返回applicantdata;
}
但问题是,在最后一行有IM错误
applicantdata = applicantList.AsQueryable()排序依据(v => v.APPLICANT_ID)。.ToList();
错误说
错误1无法隐式转换类型
System.Collections.Generic.List< AnonymousType#1>
到System.Collections.Generic .IEnumerable< Models.APPLICANT>
。一个显式转换存在(是否缺少强制)
块引用>
这是我的申请人CLASS
[DataContract(IsReference =真)]
公共部分类申请人
{
[数据成员]
公众诠释APPLICANT_ID {得到;组; }
[数据成员]
公共字符串APPLICANT_LastName {搞定;组; }
[数据成员]
公共字符串APPLICANT_FirstName {搞定;组; }
[数据成员]
公共字符串APPLICANT_MiddleName {搞定;组; }
[数据成员]
公共字符串APPLICANT_Address {搞定;组; }
[数据成员]
公共字符串APPLICANT_City {搞定;组; }
[数据成员]
公共字符串APPLICANT_Phone {搞定;组; }
[数据成员]
公共字符串APPLICANT_Email {搞定;组; }
[数据成员]
公众可空< INT> PROFILE_ID {搞定;组; }
解决方案这是藏品的类型是。问题
的IEnumerable<申请人GT; applicantdata ...
不等于你从这个表达式得到的类型:
VAR applicantList =
(从在context.Profiles
加入context.APPLICANTs
的应用程序上。 PROFILE_ID等于app.Profile_id到joined.DefaultIfEmpty()
从j加入
选择新//< - 这就是dicides的applicationList
的类型{
简介= A,
申请人= j的,
})以(1000);
这意味着你不能做到这一点:
applicantdata = applicantList ...
我想你需要做这样的事情:
applicantdata = applicantList
.SelectMany(C => c.APPLICANTs)//该行补充
.AsQueryable()排序依据(v => v.APPLICANT_ID)。.ToList();
更新
如果您使用的是我的解决方案与
的SelectMany
,你应该问自己 - 我是否提取正确的数据,当我创建applicationList < ?/ code>。
这也许是更好的..:
VAR applicantList =
(从在context.Profiles
在a.PROFILE_ID context.APPLICANTs
加入应用等于app.Profile_id到从j加入
在joined.DefaultIfEmpty()
选择j可//< - 这是申请人输入
})以(1000)。
Trying To Left Join 2 Tables
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; }
But the problem is that im having error at the last Line
applicantdata = applicantList.AsQueryable().OrderBy(v => v.APPLICANT_ID).ToList();
The error says
Error 1 Cannot implicitly convert type '
System.Collections.Generic.List<AnonymousType#1>
' to 'System.Collections.Generic.IEnumerable<Models.APPLICANT>
'. An explicit conversion exists (are you missing a cast)THIS IS MY APPLICANT CLASS
[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; }
解决方案It is the type of the collections that is the problem.
IEnumerable<APPLICANT> applicantdata ...
is not equal to the type you get from this expression:
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);
this mean that you cannot do this:
applicantdata = applicantList...
I think you need to do something like this:
applicantdata = applicantList .SelectMany(c => c.APPLICANTs) //this line added .AsQueryable().OrderBy(v => v.APPLICANT_ID).ToList();
UPDATE
If you are using my "solution" with
SelectMany
, you should be asking yourself - "Am I extracting the right data when I createapplicationList
..?"perhaps this is better..:
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);
这篇关于无法隐式转换类型“列表< AnonymousType#1>'为“IEnumerable的< Models.APPLICANT>'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!