简体   繁体   中英

Entity Framework: Collection of ComplexType

Is possible using Entity Framework to get collection of Complex Types? Here is example database query result of View:

ID | Name | CarBrand | CarModel

111 | John | Audi | A4 
222 | Andrew | BMW | X5     
222 | Andrew | Toyota | Aygo    
333 | Dani | Opel | Corsa    
333 | Dani | Volvo | C30

I would like to finally have below model in C#:

public class Owner
{
[Key]
public int Id { get; set; }

public string Name { get; set; }

public ICollection<Car> Cars { get; set; }
}

[ComplexType]
public class Car
{
public string Brand { get; set; }

public string Model { get; set; }
}

I know that I can use Complex Type by naming Car_Brand and Car_Model but then I still got each row separately.

I would like Entity Framework to use the key value and 'merge' rows with the same Id to one and put them cars to collection.

The final result should be:

[
{
 "Id": 111,
 "Name": "John",
 "Cars":
 [
    {
    "Brand": "Audi",
    "Model": "A4" 
    }
 ]
},
{
 "Id": 222,
 "Name": "Andrew",
 "Cars":
 [
    {
    "Brand": "BMW",
    "Model": "X5" 
    },
    {
    "Brand": "Toyota",
    "Model": "Aygo" 
    }
 ]
},
{
 "Id": 333,
 "Name": "Dani",
 "Cars":
 [
    {
    "Brand": "Opel",
    "Model": "Corsa" 
    },
    {
    "Brand": "Volvo",
    "Model": "C30" 
    }
 ]
}]

Thanks for help!

EDIT: The same case from diffrent side: What if I have something like this in db:

OwnerView (View)
- OwnerId
- Name
- some additional data from other tables...

OwnerCar (Table)
- OwnerId
- CarId

Car (Table)
- Id
- Brand
- Model

and I have defined relations as below:

[Table("OwnerView")]
public class Owner
{
[Key]
public int Id { get; set; }

public string Name { get; set; }

public virtual ICollection<OwnerCar> OwnerCars { get; set; }
}

public class OwnerCar {
[Key, Column(Order = 0)]
public int OwnerId { get; set; }
[Key, Column(Order = 1)]
public int CarId { get; set; }


public virtual Owner Owner { get; set; }
public virtual Car Car { get; set; }
}

public class Car
{
[Key]
public int Id { get; set; }

public string Brand { get; set; }

public string Model { get; set; }

public virtual ICollection<OwnerCar> OwnerCars { get; set; }  
}

I would like to get OwnerView with collection of his cars inside. The result should be still a Queryable.

One option would to be use a groupby statement such as

var result = from r in dataset 
    group r by new { r.Id, r.Name } into grouping
    select new Owner { 
        Id = grouping.Key.Id, 
        Name = grouping.Key.Name,
        Cars = grouping.Select(g => new Car { Brand = g.CarBrand, Model = g.CarModel }).ToList()
    };

Alternatively, If it's possible to alter your database structure you may be able to normalise the records and make use of LINQ navigation properties. This would probably involve creating an "Owners" model and a "Cars" model in your database. You would then be able to do something really simple like

var result = Owners.Include(o => o.Cars); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM