Home > Software design >  How to store multiple rows in one variable? C# Entity Framework
How to store multiple rows in one variable? C# Entity Framework

Time:11-11

public static List<TruckWithModel> GetAllTrucks()
{
    using (DAD_BaldipContext ctx = new DAD_BaldipContext())
    {
        var x = ctx.TruckFeatureAssociation
                   .Include(t => t.Truck)
                   .Include(tf => tf.Feature)
                   .Include(tm => tm.Truck.TruckModel)
                   .Select(it => new TruckWithModel()
                                 {
                                     Colour = it.Truck.Colour,
                                     Size = it.Truck.TruckModel.Size,
                                     RentalPrice = it.Truck.DailyRentalPrice,
                                     Status = it.Truck.Status,
                                     Model = it.Truck.TruckModel.Model,
                                     Rego = it.Truck.RegistrationNumber,
                                     Features = it.Feature.Description
                                 }) ;

        return (List<TruckWithModel>)x.ToList();
    }
}

This code retrieves the various attribute values from the relative tables TruckFeatureAssociation, TruckFeature, IndividualTruck and TruckModel.

The trouble I'm having is that the TruckFeatureAssociation has up to 5 entries for the same truck, this table is a junction table between IndividualTruck and TruckFeature where TruckFeature is a table of various features.

For each TruckFeatureAssociation a different object of TruckWithModel is created i.e. if there are 3 features associated each truck has three rows displayed in the datagrid where I call this function.

I want it so that all the features can be stored in one object.

enter image description here

So in the above output I would want, only one row, saying alarm systems, chrome wheels.

CodePudding user response:

The issue here is that you are querying against Features, but the model reflects a Truck... Query a truck, get it's features, then let your view model (TruckWithModel) help format that data for the view..

For example:

[Serializable]
public class TruckWithModel
{
    public string Colour { get; set; }
    public string Size { get; set; }
    public decimal RentalPrice { get; set; }
    public string Status { get; set; }
    public string Model { get; set; }
    public List<string> Features { get; set; } = new List<string>();

    public string FormattedFeatures
    {
       get { return string.Join(", ", Features); }
    }
}

Now when you query the data:

var trucks = ctx.Trucks
    .Select(t => new TruckWithModel()
    {
        Colour = t.Colour,
        Size = t.TruckModel.Size,
        RentalPrice = t.DailyRentalPrice,
        Status = t.Status,
        Model = t.TruckModel.Model,
        Rego = t.RegistrationNumber,
        Features = t.Features.Select(f => f.Description).ToList()
    }).ToList();    

This assumes that Truck has a Collection of Features where TruckFeatureAssociation is just a mapping entity. If your Truck's collection is based off TruckFeatureAssociation:

        Features = t.Features.Select(f => f.Feature.Description).ToList()

Now in your view, where you want to display the features, bind to the FormattedFeatures property to get the comma-delimited list of Features for each Truck.

Note that when you use Projection through .Select() you do not need to use .Include() to eager load related entities. EF can work out what to load to satisfy the projection automatically.

  • Related