Reputation: 989
i need help to understand linq join. i found out a few topics related with this issue but i didnt found one with a good explanation of steps.
i normal query, i do this.
var q = from c in context.tableA
select c;
List<tableA> tableAList = q.ToList();
in q.ToList() its get executed the query, right?
here found some examples but i want to be clear about this,
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders;
ObjectSet<SalesOrderDetail> details = context.SalesOrderDetails;
var query =
from order in orders
join detail in details
on order.SalesOrderID equals detail.SalesOrderID
where order.OnlineOrderFlag == true
&& order.OrderDate.Month == 8
select new
{
SalesOrderID = order.SalesOrderID,
SalesOrderDetailID = detail.SalesOrderDetailID,
OrderDate = order.OrderDate,
ProductID = detail.ProductID
};
foreach (var order in query)
{
Console.WriteLine("{0}\t{1}\t{2:d}\t{3}",
order.SalesOrderID,
order.SalesOrderDetailID,
order.OrderDate,
order.ProductID);
}
}
so from this example i can see that query can have more than 1 objetc but what about this "select new" ? is it called for each record in the DB ?
what type is that object? anyone i want or is order because is the first table in the query?
in case of the object is the first table, what happens if i need to have data that is not defined in this type of, i mean new attr.
other question, when is the query executed?
also, is this method good for response speed or are better solutions?
thx in advance. If there is a thread with this answers plz point me well.
Upvotes: 0
Views: 1579
Reputation: 2218
Select new
is creating an anonymous type, you can put whatever fields you wish from both tables into it.
You can do some reading about it here:
http://msdn.microsoft.com/en-us/library/bb397696.aspx
The query is executed whenever you use it's results, it can either happen when you iterate(foreach) over it, or whenever you call toList(), toArray() or whatever on it.
This method is rather good for response speed. As with any generated SQL, it probably can be optimized according to your use case, but unless you have a really huge amount of data, it is totally sufficient. Otherwise, you'd have to write a Stored Procedure, and map it with Entity Framework
Upvotes: 1
Reputation: 82654
in q.ToList() its get executed the query, right?
yes
what about this "select new" ? is it called for each record in the DB ?
new is just a new anonymous object, the query is ran against your table normally.
what type is that object? anyone i want or is order because is the first table in the query?
it's anonymous, you could do select new Order {
though, if you have an Order class defined.
in case of the object is the first table, what happens if i need to have data that is not defined in this type of, i mean new attr.
You would have to select it or add the attribute/property to your object/class.
other question, when is the query executed?
In the foreach loop
also, is this method good for response speed or are better solutions?
Yes, it's fine
Upvotes: 2