4

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

1
  • 1
    You haven't given the member a name. Commented Oct 4, 2013 at 17:17

1 Answer 1

11

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();
Sign up to request clarification or add additional context in comments.

4 Comments

Ok... I'm feeling really dumb xD Ty :D
@HélderGonçalves, no worries, happens to all of us :)
@HélderGonçalves: In addition, CodeCaster's comment holds as well, there is no valid type for matches as you didn't provide a name for the member of the anonymous class you were creating.
Can you show me how to do that with extended methods from linq? like .Join() :D Ty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.