Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join statement for a list

I have a table in C# and ASP.net that references a list of another table where several vehicle registrants are mapped to a single vehicle. I do not want to go through the whole collection of vehicle registrants by accessing their individual members (e.g. vehicleRegistrantsAlias[0]), because I do not know how many there will be.

Do I need to do this in two queries? I really would like to do it all in one. I have the parent class as vehicle and a child collection of vehicle registrants. Vehicle registrant is a base type and under a vehicle registrant is the owner and operator. I also want to pull information form other child tables. I am joining from vehicle to vehicleRegistrants which works fine; however, I also want to pull owner and operant.

Vehicle vehicleAlias = null;
List<VehicleRegistration> vehicleRegistrationsAlias = null; 
List<VehicleRegistrant> vehicleRegistrantsAlias = null; 

.JoinAlias(() => vehicleAlias.VehicleRegistrations, () => vehicleRegistrationsAlias)
.JoinAlias(() => vehicleAlias.VehicleRegistrants, () => vehicleRegistrantsAlias)

//from vehicle registrants
.JoinAlias(() => vehicleRegistrantsAlias[0]., () => vehicleSuspensionTypeAlias)
like image 972
joncodo Avatar asked Oct 07 '11 13:10

joncodo


People also ask

How does join () work for a list?

Note: The join() method provides a flexible way to create strings from iterable objects. It joins each element of an iterable (such as list, string, and tuple) by a string separator (the string on which the join() method is called) and returns the concatenated string.

How do you join a list in Python?

Using append() function One simple and popular way to merge(join) two lists in Python is using the in-built append() method of python. The append() method in python adds a single item to the existing list.

What is join in list?

Python join list means concatenating a list that has strings with a definite delimiter for making a string. Moreover, sometimes it's way too useful when we have to change the list to a string. For example, converting a list of the alphabets to a string separated with commas to save in a file.


1 Answers

You can use LINQ to NHibernate as well, it usually makes the queries easier to understand than the QueryOver API.

Based on the comments, I guess you want a function with the following signature:

public Vehicle GetVehicle(VehicleRegistrant registrant)
{
    var vehicle = session.Query<Vehicle>()
                         .FetchMany(x => x.VehicleRegistrants)
                         .ThenFetch(x => x.Owner)
                         .Where(x => x.VehicleRegistrants.Contains(registrant))
                         .SingleOrDefault();
}

Or, if you have a relationship in the direction from VehicleRegistrant to its parent Vehicle, this would also work

public Vehicle GetVehicle(VehicleRegistrant registrant)
{
    var vehicle = session.Query<Vehicle>()
                         .FetchMany(x => x.VehicleRegistrants)
                         .ThenFetch(x => x.Owner)
                         .Where(x => x == registrant.Vehicle)
                         .SingleOrDefault();
}

I'm not sure if this is exactly what you wanted, but I believe you get the idea. FetchMany(x => x.VehicleRegistrants) tells NHibernate to also fetch all the VehicleRegistrants for each Vehicle in the result set and the following ThenFetch(x => x.Owner) tells "and for each of those VehicleRegistrants, also fetch its Owner".

Does this help?

like image 141
twoflower Avatar answered Sep 20 '22 21:09

twoflower