将Linq转换为列表得到匿名类型错误
本文关键字:类型 错误 Linq 转换 列表 | 更新日期: 2023-09-27 18:13:01
我在S/O上看到过类似的错误,但是修复并没有直接告诉我。
Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<DTD.ManagedMPS.Core.EtchVectorShapes>'
我需要做什么来传递这个linq查询作为我的特定列表?
public List<EtchVectorShapes> GetEtchVectors(List<ViolationType> list, List<ExcelViolations> excelViolations)
{
var etchVector = new List<EtchVectorShapes>();
etchVector = (from vio in list
where excelViolations.Any(excelVio => vio.VioID.Formatted.Equals(excelVio.VioID.ToString()))
&& excelViolations.Any(excelVio => vio.RuleType.Formatted.Equals(excelVio.RuleType))
&& excelViolations.Any(excelVio => vio.VioType.Formatted.Equals(excelVio.VioType))
&& excelViolations.Any(excelVio => vio.EtchVects.Any(x => x.XCoordinate.Equals(excelVio.XCoordinate)))
&& excelViolations.Any(excelVio => vio.EtchVects.Any(y => y.YCoordinate.Equals(excelVio.YCoordinate)))
select new
{
EtchVectors = vio.EtchVects // firstOrDefault.Formatted
}).ToList();
return etchVector;
}
您需要将选择更改为select new EtchVectorShapes{EtchVectors = vio.EtchVects}
您也不需要将etchVector
变量初始化为新的List
EtchVectorShapes
是否正确映射到excelViolations
??你知道这是不同的类型吗?
也像其他答案一样,您需要在查询末尾添加.ToList()
以使其成为List<>