user2300652
user2300652

Reputation:

Convert Linq to List getting AnonymousType error

I have seen similar error on S/O but the fixes were not straight forward to me.

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<DTD.ManagedMPS.Core.EtchVectorShapes>'

What am I needing to do to pass this linq query out as my specific list?

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

Upvotes: 2

Views: 99

Answers (2)

Scott Selby
Scott Selby

Reputation: 9570

is EtchVectorShapes properly mapped to excelViolations ??? you realize that these are different types?

also like the other answers , you need to add .ToList() at the end of the query to make it into a List<>

Upvotes: 0

AlwaysAProgrammer
AlwaysAProgrammer

Reputation: 2919

you need to change the select to select new EtchVectorShapes{EtchVectors = vio.EtchVects}

You also do not need to initialize the etchVector variable to the new List.

Upvotes: 2

Related Questions