To be surprised, we can reduce the numberCodeIt saves us time, but makes some small extensions for convenience. Some content comes from the network.
Insert dB. Find <t> (t) using the object class ):
Public iqueryable <tentity> Find <tentity> (tentity OBJ) Where tentity: Class
{
// Obtain information about all properties
Propertyinfo [] properties = obj. GetType (). getproperties (bindingflags. Public | bindingflags. instance );
// Construct the initial query
Iqueryable <tentity> query = This. gettable <tentity> (). asqueryable <tentity> ();
// Traverse each property
Foreach (propertyinfo P in properties)
{
If (P! = NULL)
{
Type T = P. propertytype;
// Add object, binary, and xdocument to support ing of SQL _variant, imager, and XML.
If (T. isvaluetype | T = typeof (string) | T = typeof (system. byte [])
| T = typeof (object) | T = typeof (system. xml. LINQ. xdocument)
| T = typeof (system. Data. LINQ. Binary ))
{
// If it is not null, it is considered as a condition.
If (P. getvalue (OBJ, null )! = NULL)
{
If (columnattribute) (p. getcustomattributes (typeof (columnattrites), true) [0]). isprimarykey & convert. toint32 (P. getvalue (OBJ, null) = 0)
{
}
Else
{
// Construct a LINQ expression
Parameterexpression Param = expression. parameter (typeof (tentity), "C ");
Expression right = expression. Constant (P. getvalue (OBJ, null ));
Expression left = expression. Property (Param, P. Name );
Expression Filter = expression. Equal (left, right );
Expression <func <tentity, bool> Pred = expression. Lambda <func <tentity, bool> (filter, Param );
Query = query. Where (Pred );
}
}
}
}
}
Return query;
}
Query through the primary key of the class:
Public tentity findkey <tentity> (object Value) Where tentity: Class
{
// Obtain information about all properties
Propertyinfo [] properties = typeof (tentity). getproperties (bindingflags. Public | bindingflags. instance );
// Construct the initial query
Iqueryable <tentity> query = This. gettable <tentity> (). asqueryable <tentity> ();
// Traverse each property
Foreach (propertyinfo P in properties)
{
If (P! = NULL)
{
Type T = P. propertytype;
// Add object, binary, and xdocument to support ing of SQL _variant, imager, and XML.
If (T. isvaluetype | T = typeof (string) | T = typeof (system. byte [])
| T = typeof (object) | T = typeof (system. xml. LINQ. xdocument)
| T = typeof (system. Data. LINQ. Binary ))
{
// If it is not null, it is considered as a condition.
If (columnattribute) (p. getcustomattributes (typeof (columnattribute), true) [0]). isprimarykey)
{
Parameterexpression Param = expression. parameter (typeof (tentity), "D ");
Expression right = expression. Constant (value );
Expression left = expression. Property (Param, P. Name );
Expression Filter = expression. Equal (left, right );
Expression <func <tentity, bool> Pred = expression. Lambda <func <tentity, bool> (filter, Param );
Query = query. Where (Pred );
Break;
}
}
}
}
Return query. First ();
}
}
Update through object classes:
Public void update <tentity> (tentity OBJ) Where tentity: Class
{
String STR = "Update [" + typeof (tentity). Name + "] Set ";
String Cols = "";
String where = "";
// Obtain information about all properties
Propertyinfo [] properties = obj. GetType (). getproperties (bindingflags. Public | bindingflags. instance );
// Construct the initial query
Iqueryable <tentity> query = This. gettable <tentity> (). asqueryable <tentity> ();
// Traverse each property
Foreach (propertyinfo P in properties)
{
If (P! = NULL)
{
Type T = P. propertytype;
// Add object, binary, and xdocument to support ing of SQL _variant, imager, and XML.
If (T. isvaluetype | T = typeof (string) | T = typeof (system. byte [])
| T = typeof (object) | T = typeof (system. xml. LINQ. xdocument)
| T = typeof (system. Data. LINQ. Binary ))
{
// If it is not null, it is considered as a condition.
If (P. getvalue (OBJ, null )! = NULL)
{
If (columnattribute) (p. getcustomattributes (typeof (columnattribute), true) [0]). isprimarykey)
{
Where + = "Where [" + P. Name + "] =" + P. getvalue (OBJ, null );
}
Else
{
If (! (T. tostring (). tolower (). Contains ("string") | T. tostring (). tolower (). Contains ("datetime ")))
Cols + = "[" + P. Name + "] =" + P. getvalue (OBJ, null) + ",";
Else
Cols + = "[" + P. Name + "] = '" + P. getvalue (OBJ, null) + "',";
}
}
}
}
}
STR + = cols. substring (0, cols. Length-1) + where;
Httpcontext. Current. response. Write ("<br>" + STR + "<br> ");
This. executecommand (STR );
}
Public void updateall <tentity> (ienumerable <tentity> OBJ) Where tentity: Class
{
Foreach (VAR item in OBJ)
{
This. Update <tentity> (item );
}
}
Similar to updating, deleting, and adding code, you can also perform data operations on multiple data entries. The code is similar to the update. The db. CS file in the Code http://fccms.googlecode.com/files/linqtoaccess.rar.