I have a stored procedure returning results in a FlaggedCarDTO object:
public class FlaggedCarDTO
{
public int CarId { get; set; }
public int FlagUserId { get; set; }
public int Year { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int ImageId { get; set; }
public string ImageName { get; set; }
}
Here is some sample data:
var query = new List<FlaggedCarDTO>()
{
new FlaggedCarDTO() { CarId = 1, FlagUserId = 22, Year = 2020, Make = "Toyota", Model = "Camry", ImageId = 1, ImageName = "01.jpg" },
new FlaggedCarDTO() { CarId = 1, FlagUserId = 22, Year = 2020, Make = "Toyota", Model = "Camry", ImageId = 2, ImageName = "02.jpg" },
new FlaggedCarDTO() { CarId = 1, FlagUserId = 46, Year = 2020, Make = "Toyota", Model = "Camry", ImageId = 1, ImageName = "01.jpg" },
new FlaggedCarDTO() { CarId = 1, FlagUserId = 46, Year = 2020, Make = "Toyota", Model = "Camry", ImageId = 2, ImageName = "02.jpg" },
new FlaggedCarDTO() { CarId = 6, FlagUserId = 67, Year = 2016, Make = "Honda", Model = "Civic", ImageId = 18, ImageName = "54.jpg" },
new FlaggedCarDTO() { CarId = 6, FlagUserId = 67, Year = 2016, Make = "Honda", Model = "Civic", ImageId = 19, ImageName = "55.jpg" }
};
I want to condense this data to display only the first record for each CarId into this objects:
public class FlaggedCar
{
public int Id { get; set; }
public int FlagUserId { get; set; }
public int Year { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public List<Image> Images { get; set; }
}
public class Image
{
public int Id { get; set; }
public string Name { get; set; }
}
Here is my LINQ query:
var result = query.GroupBy(x => x.CarId)
.Select(grp => grp.Select(c => new FlaggedCar()
{
Id = c.CarId,
FlagUserId = c.FlagUserId,
Year = c.Year,
Make = c.Make,
Model = c.Model,
Images = grp.Select(i => new Image()
{
Id = i.ImageId,
Name = i.ImageName,
}).ToList(),
}).First()).ToList();
Here is the result (serialized for readability):
[
{
"Id": 1,
"FlagUserId": 22,
"Year": 2020,
"Make": "Toyota",
"Model": "Camry",
"Images": [
{
"Id": 1,
"Name": "01.jpg"
},
{
"Id": 2,
"Name": "02.jpg"
},
{
"Id": 1,
"Name": "01.jpg"
},
{
"Id": 2,
"Name": "02.jpg"
}
]
},
{
"Id": 6,
"FlagUserId": 67,
"Year": 2016,
"Make": "Honda",
"Model": "Civic",
"Images": [
{
"Id": 18,
"Name": "54.jpg"
},
{
"Id": 19,
"Name": "55.jpg"
}
]
}
]
This is almost what I need, but the Images are not selecting only from the first record. Note that the Camry, the images include from both records. I think a .First() is needed on the nested Images list but I'm not sure where to add it?