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.
yes
new is just a new anonymous object, the query is ran against your table normally.
it’s anonymous, you could do
select new Order {though, if you have an Order class defined.You would have to select it or add the attribute/property to your object/class.
In the foreach loop
Yes, it’s fine