我正在尝试加入LINQ& LAMBDA。但是获取错误声明无法将匿名类型#1隐式转换为BRAND_NAME_MAP_ID。我对Lambdas很新,并且坚持到这里。
public BRAND_NAME_MAP_MASTER GetBrandNameBrandNameMapID(int BrandNameMapID)
{
PFC_BRAND_NAME_MAP_MASTER objBrandNameMap = Db.PFC_BRAND_NAME_MAP_MASTERs
.Join(SIRDC.INGREDIENT_BRAND_NAME_MAPs,
a => a.BRAND_NAME_MAP_ID,
b => b.BRAND_NAME_MAP_ID,
(a, b) => new {a, b})
.Where(x => x.a.BRAND_NAME_MAP_ID == PFCBrandNameMapID)
.FirstOrDefault(); //Getting error here
return objPFCBrandNameMap;
}
答案 0 :(得分:4)
您的查询返回包含两个属性的匿名类型,a
为PFC_BRAND_NAME_MAP_MASTER
,b
为INGREDIENT_BRAND_NAME_MAP
。看起来你只需要第一个属性,所以只需添加一个Select
来获取它:
PFC_BRAND_NAME_MAP_MASTER objBrandNameMap = Db.PFC_BRAND_NAME_MAP_MASTERs
.Join(SIRDC.INGREDIENT_BRAND_NAME_MAPs,
a => a.BRAND_NAME_MAP_ID,
b => b.BRAND_NAME_MAP_ID,
(a, b) => new {a, b})
.Where(x => x.a.BRAND_NAME_MAP_ID == PFCBrandNameMapID)
.Select(x => x.a) /Add this line
.FirstOrDefault();