I have a store procedure that returns an N amount of rows.
For the sake of simplicity in my backend is this POCO that will map:
public class Car
{
public int Id { get; set; }
public string Brand { get; set; }
public int OwnerId{ get; set;}
}
At the same time I also have the following POCO:
public class Owner
{
public int Id { get; set; }
public string FirstName { get; set; }
}
Now what I want to achieve is the following, having the list:
List<Car> lstCar = db.sp.AllMyCars()
.Select(car => new Car()
{
Id = car.Id,
Brand = car.Brand,
OwnerId = car.OwnerId
}).ToList();
Now I want to save in a dictionary of type Dictionary<int,List<Owner>>
EDIT:
To clarify, you already have a list of Cars List<Car>
as lstCar
, now you want to group them by Id
and a list of Owners, which we will need to create a list of owners and for each element of list of car we will need to add and fill this lists as Cars (you will only fill the id, its ok, it doesnt matter).
Example:
your int in your dictionary will be "Number" and the only field that you will fill in your List is the OwnerId.
So you will want to group by the Number a list of Owner Objects, in this case for example number 44 would return 5 Owner Objects with their Id as 2,3,4,5 and 6.
,int>` - can you please clarify what you want to achieve with that and show an example of the dictionary?
– Alexei Levenkov Feb 21 '19 at 19:10