Frage

I need to print out a Key which could have multiple values. But I need to print this based on Index. For e.g. if I pass index 0, it should check for A and then print out it's values. Like a 1 to n thing.

Even my below example is printing out the first, as I'm using the .FIRST(). How do I print 1 to N. 1 key with multiple values.

    recordList = new List<MyData>();
      MyData add = new MyData { Data1 = "A", Data2 = "A is for Apple" };
        recordList.Add(add);
        MyData add1 = new MyData { Data1 = "A", Data2 = "B is for Ball" };
        recordList.Add(add1);
        MyData add2 = new MyData { Data1 = "A", Data2 = "C is for Cat" };
        recordList.Add(add2);
        MyData add3 = new MyData { Data1 = "A", Data2 = "D is for Doll" };
        recordList.Add(add3);

    MyData add4 = new MyData { Data1 = "B", Data2 = "A is for Apple" };
        recordList.Add(add4);
        MyData add5 = new MyData { Data1 = "B", Data2 = "B is for Ball" };
        recordList.Add(add5);
        MyData add6 = new MyData { Data1 = "B", Data2 = "C is for Cat" };
        recordList.Add(add6);
        MyData add7 = new MyData { Data1 = "B", Data2 = "D is for Doll" };
        recordList.Add(add7);



var data = recordList.AsParallel().ToLookup(x => x.Data1, (x) =>
         {
             return (from m in recordList where m.Data1 == x.Data1 select new MyData { Data2 = m.Data2, Data1 = m.Data1 }).First();

         });

  var output = data["A"];
            foreach(var print in output)
            {
                Console.WriteLine(print.Data1 + "   "+ print.Data2);
            }

The result I want to Acheive is :

A    A is for Apple
     B is for Ball
     C is for Cat
     D is for Doll

Because instead of printing this I want to pass this to another method. SO I should I have one key with it's corresponding values.

War es hilfreich?

Lösung

Just group items by Data1 property:

var groups = recordList.GroupBy(d => d.Data1);

Then you will have items grouped by their Data1 value.

foreach(var data1Group in groups)
{
    Console.WriteLine(data1Group.Key); // group key A or B

    foreach(var data in data1Group)
        Console.WriteLine(data.Data2);
}

Or use lookup

var lookup = recordList.ToLookup(d => d.Data1, d => d.Data2);

// getting items for A
foreach(var data2 in lookup["A"])
   Console.WriteLine(data2);

You can skip projecting lookup items

var lookup = recordList.ToLookup(d => d.Data1);

In this case lookup will be like a groups, but with indexed access by key.


Whats wrong in your code? Lets see what it does

 var data = recordList.ToLookup(x => x.Data1, (x) =>
 {
     return (from m in recordList 
             where m.Data1 == x.Data1 
             select new MyData { Data2 = m.Data2, Data1 = m.Data1 }).First();
 });

It groups items in recordList by Data1 property value, and for each item in group it puts into lookup following value:

  (from m in recordList 
   where m.Data1 == x.Data1 
   select new MyData { Data2 = m.Data2, Data1 = m.Data1 }).First()

Result of this query will be same for all items in group - it goes to recordList finds first item with same Data1 as current item (but not same item!) and creates its copy. Thus you end up with lookup filled with copies of first items in each group.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top