Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 440 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Group by in LINQ

#1
Let's suppose if we have a class like:

class Person {
internal int PersonID;
internal string car;
}

I have a list of this class: `List<Person> persons;`

And this list can have multiple instances with same `PersonID`s, for example:

persons[0] = new Person { PersonID = 1, car = "Ferrari" };
persons[1] = new Person { PersonID = 1, car = "BMW" };
persons[2] = new Person { PersonID = 2, car = "Audi" };

Is there a way I can group by `PersonID` and get the list of all the cars he has?

For example, the expected result would be

class Result {
int PersonID;
List<string> cars;
}

So after grouping, I would get:

results[0].PersonID = 1;
List<string> cars = results[0].cars;

result[1].PersonID = 2;
List<string> cars = result[1].cars;

From what I have done so far:

var results = from p in persons
group p by p.PersonID into g
select new { PersonID = g.Key, // this is where I am not sure what to do

Could someone please point me in the right direction?
Reply

#2
var results = from p in persons
group p by p.PersonID into g
select new { PersonID = g.Key,
/**/car = g.Select(g=>g.car).FirstOrDefault()/**/}
Reply

#3
Try this :

var results= persons.GroupBy(n => n.PersonId)
.Select(g => new {
PersonId=g.Key,
Cars=g.Select(p=>p.car).ToList())}).ToList();

But performance-wise the following practice is better and more optimized in memory usage (when our array contains much more items like millions):

var carDic=new Dictionary<int,List<string>>();
for(int i=0;i<persons.length;i++)
{
var person=persons[i];
if(carDic.ContainsKey(person.PersonId))
{
carDic[person.PersonId].Add(person.car);
}
else
{
carDic[person.PersonId]=new List<string>(){person.car};
}
}
//returns the list of cars for PersonId 1
var carList=carDic[1];



Reply

#4
var results = from p in persons
group p by p.PersonID into g
select new { PersonID = g.Key, Cars = g.Select(m => m.car) };
Reply

#5
I have created a working code sample with Query Syntax and Method Syntax. I hope it helps the others :)

[You can also run the code on .Net Fiddle here:][1]

using System;
using System.Linq;
using System.Collections.Generic;

class Person
{
public int PersonId;
public string car ;
}

class Result
{
public int PersonId;
public List<string> Cars;
}

public class Program
{
public static void Main()
{
List<Person> persons = new List<Person>()
{
new Person { PersonId = 1, car = "Ferrari" },
new Person { PersonId = 1, car = "BMW" },
new Person { PersonId = 2, car = "Audi"}
};

//With Query Syntax

List<Result> results1 = (
from p in persons
group p by p.PersonId into g
select new Result()
{
PersonId = g.Key,
Cars = g.Select(c => c.car).ToList()
}
).ToList();

foreach (Result item in results1)
{
Console.WriteLine(item.PersonId);
foreach(string car in item.Cars)
{
Console.WriteLine(car);
}
}

Console.WriteLine("-----------");

//Method Syntax

List<Result> results2 = persons
.GroupBy(p => p.PersonId,
(k, c) => new Result()
{
PersonId = k,
Cars = c.Select(cs => cs.car).ToList()
}
).ToList();

foreach (Result item in results2)
{
Console.WriteLine(item.PersonId);
foreach(string car in item.Cars)
{
Console.WriteLine(car);
}
}
}
}

Here is the result:
<pre>
1
Ferrari
BMW
2
Audi
-----------
1
Ferrari
BMW
2
Audi

</pre>

[1]:

[To see links please register here]

Reply

#6
try

persons.GroupBy(x => x.PersonId).Select(x => x)

or

to check if any person is repeating in your list try

persons.GroupBy(x => x.PersonId).Where(x => x.Count() > 1).Any(x => x)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through