简体   繁体   中英

join in linq with entity framework

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM