In EF, table joins are commonly used with join () and include (), both of which can be connected to two tables, but different.
For example, there is a record sheet album (Albumid,name,createdate,genreid), the table contains foreign keys genreid connected genre table genre (Genreid,name). Each record belongs to only one genre, and one genre can correspond to multiple albums.
1.Join (), the two tables do not have to contain foreign key relationships, you need code to manually specify the connection foreign key equality (with extensibility, in addition to the value is equal, you can also specify is >,< and other two tables of the corresponding key relationship), as well as the result field.
Overloaded mode (is the extension method, the first parameter with this, which represents itself):
1.public static iqueryable<tresult> Join<touter, Tinner, TKey, tresult> (This iqueryable<touter> Outer, ienumerable<tinner> inner, Expression<func<touter, tkey>> Outerkeyselector, Expression< Func<tinner, tkey>> innerkeyselector, Expression<func<touter, Tinner, TResult>> resultSelector) ; 2.public static iqueryable<tresult> Join<touter, Tinner, TKey, tresult> (This iqueryable<touter> Outer, ienumerable<tinner> inner, Expression<func<touter, tkey>> Outerkeyselector, Expression< Func<tinner, tkey>> innerkeyselector, Expression<func<touter, Tinner, TResult>> ResultSelector, Iequalitycomparer<tkey> comparer);
So you can write a connection of two tables:
var Wholerecord = DC. Album.join (DC. Genre, a = a.genreid, G = G.genreid, (A, g) = = new {a.albumid,a.name,g.genreid,g.name;
This selects the Albumid,name,genreid,name in addition to the two tables.
2.Include (), the two tables must contain foreign key relationships, just specify the class property name corresponding to the key name, without specifying the result field (that is, all mappings). By default, when a table is searched, the foreign key table is not queried, and the database query is not read until it is actually used, and if Include () is used, the specified foreign key table information is read when the table is read.
Overload mode:
Located in namespace System.Data.Entity.Infrastructurepublic dbquery<tresult> Include (string path);//located in namespace System.Data.Entity, be sure to introduce to find this method. Otherwise only see the previous method public static iqueryable<t> include<t, tproperty> (this iqueryable<t> source, Expression <func<t, tproperty>> Path) where T:class; public static iqueryable<t> include<t> (this iqueryable<t> source, string path) where T:class;
It can be written like this:
EF has generated the database mapping model classes for album and genre as well as the navigation properties Var wholerecord=dc. Album.include ("Genre");//or//var WHOLERECORD=DC. Album.include (a=>genre);
The database then executes a left connection, linking all the fields of album and genre, and the include () is immediately queried, like ToList (), and will not delay optimizations later.
This is actually very inefficient, because if the two tables are recorded very large, then the connection is a time-consuming resource, it is recommended to use less, or first filter out the desired result set and then connect.
Table left Join method for EF include and join