locked
Linq Dictionary doesn't return Dictionary

    Question

  • I have a Dictionary> and want to extract all the elements out of it that have more than none XElements in the Value.

    Code Snippet

    Dictionary<string, IEnumerable<XElement>> problemDict;

    return(

    from p in problemDict

    where p.Value.Count() > 0
    select p);

     

    This returns an IEnumerable of KeyValuePairs instead of a Dictionary.

    I figured the .ToDictionary() method is the key to this problem, but I can't get my brain around that one.

     

    So: why doesn't it return a Dictionary (the type of it's source) and how can I alter it so it does?

    Tuesday, March 25, 2008 11:02 AM

Answers

  •  

    You are correct in that you are after the ToDictionary method to solve your problem.

     

    You need to tell that function what to use as key and what to use as value. The easiest way to do that is to use a lambda expression, like:

     

    return (

    from p in problemDict

    where p.Value.Count() > 0

    select p).ToDictionary(item => item.Key, item => item.Value);

     

    I hope this helps.

    Tuesday, March 25, 2008 12:00 PM
  •  

    Boernoes:

     

    You need two little lambdas that can tell ToDictionary() what to use as the key and the value for each KeyValuePair coming out of the query. In other words something like:


     

    Dictionary<string, IEnumerable<XElement>> problemDict = ...;
    Dictionary<string, IEnumerable<XElement>> result;

    result =
        (
    from p in problemDict
        
    where p.Value.Count() > 0
        
    select p).ToDictionary(kv => kv.Key, kv => kv.Value);

     

     

    Hope that helps,

    Tuesday, March 25, 2008 12:19 PM

All replies

  •  

    You are correct in that you are after the ToDictionary method to solve your problem.

     

    You need to tell that function what to use as key and what to use as value. The easiest way to do that is to use a lambda expression, like:

     

    return (

    from p in problemDict

    where p.Value.Count() > 0

    select p).ToDictionary(item => item.Key, item => item.Value);

     

    I hope this helps.

    Tuesday, March 25, 2008 12:00 PM
  •  

    Boernoes:

     

    You need two little lambdas that can tell ToDictionary() what to use as the key and the value for each KeyValuePair coming out of the query. In other words something like:


     

    Dictionary<string, IEnumerable<XElement>> problemDict = ...;
    Dictionary<string, IEnumerable<XElement>> result;

    result =
        (
    from p in problemDict
        
    where p.Value.Count() > 0
        
    select p).ToDictionary(kv => kv.Key, kv => kv.Value);

     

     

    Hope that helps,

    Tuesday, March 25, 2008 12:19 PM
  • Instead of

    Code Snippet

    where p.Value.Count() > 0

     

     

    I suggest you use

    Code Snippet

    where p.Value.Any()

     

    That should be more efficient than counting the elements.

    Tuesday, March 25, 2008 12:52 PM
  • Thanks guys,

     

    Those are all helpfull posts.

    I'll check it out today.

     

    But still one question remains:

    Why doesn't linq return a dict when it gets a dict...

    I know linq is about clear code not about performance, but wouldn't it be a boost if all that casting wasn't nessecary?

    Wednesday, March 26, 2008 7:58 AM
  • The LINQ to Object functionality works with IEnumerable<T> and it does not understand Dictornary types natively. That's why there is an impedence missmatch for dictionarys.

    Wednesday, March 26, 2008 8:48 AM
  • I C,

     

    Thanks again for the replies.

    I just implemented it and it worked perfectly.

     

    Way better then getting the result, and turning it back into dictionary by foreach with an extra dictionary Embarrassed

     

     

    Wednesday, March 26, 2008 9:46 AM
  • A little late-coming, perhaps, but I have also been a little annoyed with LINQ return types when using Dictionaries. I have created a few helper extension methods and put them in a Github repository. You can find the project here. There is also a more detailed rationale for the provided extension methods in this blog post. I hope this can be of some help for anyone reading this forum.

    Regards,

    Anders @ Cureos

    Friday, October 14, 2011 6:54 AM