将Linq转换为List获取AnonymousType错误

时间:2013-05-11 00:13:34

标签: c# linq

我在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;
    }

2 个答案:

答案 0 :(得分:2)

您需要将选择更改为select new EtchVectorShapes{EtchVectors = vio.EtchVects}

您也无需将etchVector变量初始化为新列表。

答案 1 :(得分:0)

EtchVectorShapes是否正确映射到excelViolations ???你意识到这些是不同的类型?

与其他答案一样,您需要在查询末尾添加.ToList()以使其成为列表&lt;&gt;