不能隐式地将anonymoustype#1转换为Object

时间:2018-04-27 15:23:46

标签: c# asp.net linq lambda type-conversion

我正在尝试加入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;
}

1 个答案:

答案 0 :(得分:4)

您的查询返回包含两个属性的匿名类型,aPFC_BRAND_NAME_MAP_MASTERbINGREDIENT_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();