Linq to sql select new string

14,043

You are trying to select an anonymous type and then you are trying to assign the result back to List<string>. Your query should be:

List<String> matches = (from b in MyObject.classBList
                        from c in b.classCList
                        join d in dict
                        on c.ID equals d.Key
                        select d.Value == 0 ? "YES" : "NO").ToList();
Share:
14,043
Hélder Gonçalves
Author by

Hélder Gonçalves

Software Engineer with interest in .Net, C# and PL/SQL.

Updated on July 18, 2022

Comments

  • Hélder Gonçalves
    Hélder Gonçalves almost 2 years

    So i got this code:

    ClassA { List<ClassB> classBList;}
    ClassB { List<ClassC> classCList;}
    ClassC { String ID;}
    
    Dictionary<string,int> dict;
    ClassA MyObject;
    

    The keys on the dictionary should match the the ID field on ClassC And i wanna do the following query on linq

    List<String> matches = (from b in MyObject
                          from c in b.classCList
                          join d in dict
                          on c.ID equals dict.Key
                          select new
                          {
                              c.Value == 0 ? "YES" : "NO"
                          }).ToList();
    

    But I'm getting the error: "Invalid anonymous type member ..."

    The bottomline is ... how can i have a condition within the select new?

    EDIT

    How can i do this query with extended methods?

    Any help? Ty