It is very difficult to fully simulate expressions, because C # currently does not support custom operators and only supports Operator Overloading (C # is expected to provide operator customization. If a language supports this, please be advised) The idea is actually very simple. It is to set up your own type and reload some operators. The purpose of simulating this is to avoid writing Database SQL statements directly under the entity model, using ESQL, to achieve this, the Linq syntax provides the development of a consistent entity application model:
1) parameter class, used to pass the actual parameters and convert them to the required parameters in the ADO. Net command:
[Csharp]
Public class Parameter
{// No specific implementation. You only need to simulate
}
2) expression interface:
[Csharp]
Public interface IDbExpression
{
String Expression {get ;}
Dictionary <string, Parameter> Parameters {get ;}
}
3) expression base class: mainly involves some operator symbol overloading, and some areas where expressions cannot be used to define similar functions
[Csharp]
Public class DbExpression: IDbExpression
{
Private StringBuilder _ sqlStatement;
Private Dictionary <string, Parameter> _ Parameters;
Public StringBuilder SQL {get {return _ sqlStatement ;}}
Public DbExpression ()
{
_ SqlStatement = new StringBuilder ();
_ Parameters = new Dictionary <string, Parameter> ();
}
Public static DbExpression operator> (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + ">" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator <(DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "<" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator> = (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "> =" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator <= (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "<=" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator = (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "=" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator! = (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "! = "+ E2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator & (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "and" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public static DbExpression operator | (DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression ();
TheExp. SQL. AppendLine (e1.Expression + "or" + e2.Expression );
TheExp. AddParams (e1 );
TheExp. AddParams (e2 );
Return theExp;
}
Public DbExpression Parentheses (DbExpression e)
{
This. SQL. AppendLine ("(" + e. Expression + ")");
This. AddParams (e );
Return this;
}
Public DbExpression Where (DbExpression e1)
{
This. _ sqlStatement. AppendLine ("WHERE" + e1.Expression );
This. AddParams (e1 );
Return this;
}
Public DbExpression From (params DbExpression [] Tables)
{
This. SQL. AppendLine ("FROM ");
If (Tables! = Null)
{
Int I = 0;
Foreach (var item in Tables)
{
If (I = 0)
{
_ SqlStatement. Append ("" + item. Expression );
}
Else
{
_ SqlStatement. Append ("," + item. Expression );
}
This. AddParams (item );
}
}
Return this;
}
Public DbExpression Select (params DbExpression [] Fields)
{
_ SqlStatement. Append ("SELECT ");
If (Fields! = Null)
{
Int I = 0;
Foreach (var item in Fields)
{
If (I = 0)
{
_ SqlStatement. Append ("" + item. Expression );
}
Else
{
_ SqlStatement. Append ("," + item. Expression );
}
This. AddParams (item );
}
}
Else
{
_ SqlStatement. Append ("*");
}
Return this;
}
Public void AddParams (DbExpression e)
{
If (e. Parameters = null)
{
Return;
}
Foreach (var p in e. _ Parameters)
{
If (this. _ Parameters. ContainsKey (p. Key) = false)
{
This. _ Parameters. Add (p. Key, p. Value );
}
}
}
Public void AddParams (IDbExpression e)
{
If (e. Parameters = null)
{
Return;
}
Foreach (var p in e. Parameters)
{
If (this. _ Parameters. ContainsKey (p. Key) = false)
{
This. _ Parameters. Add (p. Key, p. Value );
}
}
}
Public virtual string Expression
{
Get {return _ sqlStatement. ToString ();}
}
Public virtual Dictionary <string, Parameter> Parameters
{
Get {return _ Parameters ;}
}
}
From hawksoft