类型1之间未定义任何强制运算符

时间:2018-08-16 03:12:12

标签: c#

 var cpaclassids = FetchProductTypes(ids);
 var cpaclids = string.Join(",", cpaclassids);            
 var query = (from x in partJoinTableRepository.GetPartJoinQuery() join y in partRepository.GetPartsQuery() on x.PartId equals y.Id
                                      join z in partProductTypeReposiotry.GetPartProductTypesQuery() on x.PartId equals z.PartId where y.IsSkipped == 0 && (y.IsDisabled != "Y" || y.IsDisabled == null) && z.CreatedDate == x.CreatedDate && x.CreatedDate == Convert.ToDateTime(fromDate) select new { x }).Cast<PartJoinTable>();

 var predicate1 = PredicateBuilder.True(query);
 predicate1 = predicate1.And(x => cpaclassids.Contains(x.ProductTypeId.ToString()));

 int lst = query.Where(predicate1).Select(x => x.PartId).Distinct().ToList().Count(); // on this line

异常详细信息:

  

在类型'<> f__AnonymousType5`1 [PartDomain.Model.Models.PartJoinTable]'和'PartDomain.Model.Models.PartJoinTable'之间没有定义强制运算符。

当我尝试一次获取计数时会显示异常

1 个答案:

答案 0 :(得分:2)

select new { x }

返回一个匿名类型,其匿名类型为x。不出所料,该匿名类型无法转换为PartJoinTable

解决方法是将其更改为:

PartJoinTable

取而代之的是返回select x 实体自身

PartJoinTable