Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problems Using ToDictionary Extension Method

Problems Using ToDictionary Extension Method

Scheduled Pinned Locked Moved C#
helpcsharplinqquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dlarkin77
    wrote on last edited by
    #1

    Hi, I have a dictionary of items. I want to create a new Dictionary by taking items from it where each item meets a particular criterion. I figure that the ToDictionary extension method would suit my purpose, but for the life of me I cannot get it to compile.

    Dictionary test = new Dictionary{
    {1 ,"a"},{2, "b"},{3 ,"c"},{4, "d"}
    };

    Dictionary d = test.ToDictionary(o => o.Value.CompareTo("d") == -1);
    // compiler error on this line
    //'System.Collections.Generic.Dictionary' does not contain a definition for 'ToDictionary' and the best extension method overload //'System.Linq.Enumerable.ToDictionary(System.Collections.Generic.IEnumerable, System.Func)' has some //invalid arguments

    The only thing that I could manage that came close to what I need is:

    List> t1 = test
    .Where(o => o.Value.CompareTo("d") == -1)
    .ToList>();

    I reckon I'm doing something really stupid but I can't see what it is. Can anyone help me out? Thanks very much, dlarkin77

    L 1 Reply Last reply
    0
    • D dlarkin77

      Hi, I have a dictionary of items. I want to create a new Dictionary by taking items from it where each item meets a particular criterion. I figure that the ToDictionary extension method would suit my purpose, but for the life of me I cannot get it to compile.

      Dictionary test = new Dictionary{
      {1 ,"a"},{2, "b"},{3 ,"c"},{4, "d"}
      };

      Dictionary d = test.ToDictionary(o => o.Value.CompareTo("d") == -1);
      // compiler error on this line
      //'System.Collections.Generic.Dictionary' does not contain a definition for 'ToDictionary' and the best extension method overload //'System.Linq.Enumerable.ToDictionary(System.Collections.Generic.IEnumerable, System.Func)' has some //invalid arguments

      The only thing that I could manage that came close to what I need is:

      List> t1 = test
      .Where(o => o.Value.CompareTo("d") == -1)
      .ToList>();

      I reckon I'm doing something really stupid but I can't see what it is. Can anyone help me out? Thanks very much, dlarkin77

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This will work for you:

      IDictionary<int,string> d = (
      from t in test
      where t.Value.CompareTo("d") == -1
      select t).
      ToDictionary(item => item.Key, item => item.Value);

      regards

      D 1 Reply Last reply
      0
      • L Lost User

        This will work for you:

        IDictionary<int,string> d = (
        from t in test
        where t.Value.CompareTo("d") == -1
        select t).
        ToDictionary(item => item.Key, item => item.Value);

        regards

        D Offline
        D Offline
        dlarkin77
        wrote on last edited by
        #3

        That works lovely. Thank very much.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups