database - Pivot-type scenario with objects in C# -


consider scenario

i have class called user contains list of car types (e.g toyota, bmw)

so user can have many cars.

let's have list of type user. in current format, can find cars user owns.

what cannot do, find users own particular car type (e.g users own toyota).

in classical database sense want able pivot data, how 1 achieve such operation when working objects in c#?

thanks thomas

you have one-to-many relationship can go both ways. cars have users , users have cars. see following c# code:

class users {     public string name { get; set; }      public list<car> cars { get; set; } }  class car {     public string name { get; set; } }  var users = new list<users>                     {                         new users                             {                                 name = "bob",                                 cars = new list<car> { new car { name = "toyota" } }                             }                     };  // cars name "toyota" in user's list of cars.     var userswithcar = users.where(user => user.cars.any(car => car.name == "toyota"));   string username = userswithcar.single().name; // bob 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -