110,534
社区成员
发帖
与我相关
我的任务
分享
public class FieldDesc<T>{
private string _fldName = "";
private T _fldValue;
public FieldDesc(string name){
_fldName = name;
}
public FieldDesc(T value){
_fldValue = value;
}
public string fldName{
get{return _fldName;}
set
{_fldName = value;}
}
public T fldValue{
get{return _fldValue;}
set{_fldValue = value;}
}
public override string ToString(){
return _fldValue.ToString();
}
public static implicit operator FieldDesc<T>(T value){
return new FieldDesc<T>(value);
}
}
class Program{
static void Main(string[] args){
FieldDesc<int> UserID = new FieldDesc<int>("UserID");
FieldDesc<string> UserName = new FieldDesc<string>("UserName");
FieldDesc<string> Address = new FieldDesc<string>("Address");
UserName = "张三"; //如果保留UserName的fldName属性值???
Console.WriteLine(UserName.fldName);
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace AnNet.Entity
{
/// <summary>
/// 构造一个Expression表达式对象
/// </summary>
public class Expression
{
/// <summary>
/// 构造一个Expression对象
/// </summary>
/// <param name="PropertyName">属性名称</param>
/// <param name="BaseEntityMap">实体映射</param>
internal Expression(string PropertyName, BaseEntityMap BaseEntityMap)
{
this.PropertyName = PropertyName;
this.BaseEntityMap = BaseEntityMap;
}
/// <summary>
/// 实体映射
/// </summary>
internal BaseEntityMap BaseEntityMap = null;
/// <summary>
/// 名称
/// </summary>
string PropertyName = null;
/// <summary>
/// 条件
/// </summary>
public string Condition = "";
/// <summary>
/// 参数集合
/// </summary>
public List<EntityProperty> EntityPropertys = new List<EntityProperty>();
private void AddExpression(Expression expression, string equals)
{
if (expression.EntityPropertys.Count == 0)
{
this.Condition = this.ToString() + " " + equals + " " + expression.ToString();
return;
}
for (int i = 0; i < expression.EntityPropertys.Count; i++)
{
EntityProperty p = expression.EntityPropertys[i];
//条件
string Condition = expression.Condition;
//参数名称
string ParamsName = p.PropertyName;
//新参数名称
string _ParamsName = "p" + this.EntityPropertys.Count;
if (this.IsExist(p.PropertyName))
{
Condition = Condition.Replace("@" + ParamsName, "@" + _ParamsName);
}
if (equals == "=")
{
this.Condition = this.ToString() + " = " + expression;
}
else
{
this.Condition = "(" + this.Condition + ")" + " " + equals + " " + "(" + Condition + ")";
}
p.PropertyName = _ParamsName;
this.EntityPropertys.Add(p);
}
}
private bool IsExist(string paramsName)
{
foreach (EntityProperty p in this.EntityPropertys)
{
if (paramsName == p.PropertyName)
{
return true;
}
}
return false;
}
public static Expression operator ==(Expression compare, object Value)
{
if (Value is Expression)
{
Expression compare1 = compare;
Expression compare2 = Value as Expression;
compare1.AddExpression(compare2, "=");
return compare1;
}
return BuildOperator(compare, Value, " = ");
}
public static Expression operator |(Expression compare1, Expression compare2)
{
compare1.AddExpression(compare2, "OR");
return compare1;
}
public static Expression operator &(Expression compare1, Expression compare2)
{
compare1.AddExpression(compare2, "AND");
return compare1;
}
public static Expression operator !=(Expression compare, object Value)
{
if (Value is Expression)
{
Expression compare1 = compare;
Expression compare2 = Value as Expression;
compare1.AddExpression(compare2, "<>");
return compare1;
}
return BuildOperator(compare, Value, " <> ");
}
public static Expression operator >(Expression compare, object Value)
{
if (Value is Expression)
{
Expression compare1 = compare;
Expression compare2 = Value as Expression;
compare1.AddExpression(compare2, ">");
return compare1;
}
return BuildOperator(compare, Value, " > ");
}
public static Expression operator >=(Expression compare, object Value)
{
if (Value is Expression)
{
Expression compare1 = compare;
Expression compare2 = Value as Expression;
compare1.AddExpression(compare2, ">=");
return compare1;
}
return BuildOperator(compare, Value, " >= ");
}
public static Expression operator <(Expression compare, object Value)
{
if (Value is Expression)
{
Expression compare1 = compare;
Expression compare2 = Value as Expression;
compare1.AddExpression(compare2, "<");
return compare1;
}
return BuildOperator(compare, Value, " < ");
}
public static Expression operator <=(Expression compare, object Value)
{
if (Value is Expression)
{
Expression compare1 = compare;
Expression compare2 = Value as Expression;
compare1.AddExpression(compare2, "<=");
return compare1;
}
return BuildOperator(compare, Value, " <= ");
}
private static Expression BuildOperator(Expression compare, object Value, string operatorString)
{
string paramsName = "p" + compare.EntityPropertys.Count;
//列名称
compare.Condition += compare.ToString() + operatorString + "@" + paramsName;
EntityProperty dp = compare.BaseEntityMap.Propertys[compare.ColumnName];
EntityProperty property = new EntityProperty(paramsName, Value, dp.Size, dp.DbType, dp.IsPrimaryKey, dp.IsIdEntity, dp.AutoSync);
compare.EntityPropertys.Add(property);
return compare;
}
internal string ColumnName
{
get
{
string temp = this.PropertyName.Substring(this.PropertyName.IndexOf('.') + 1, this.PropertyName.Length - this.PropertyName.IndexOf('.') - 1);
return temp;
}
}
#region Override Object
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override string ToString()
{
return this.PropertyName;
}
#endregion
#region SQLFunction
/// <summary>
/// 求和统计
/// </summary>
/// <returns>求和统计属性</returns>
public Expression Sum()
{
string temp = this.PropertyName.Substring(this.PropertyName.IndexOf('.') + 1, this.PropertyName.Length - this.PropertyName.IndexOf('.') - 1);
return new Expression("SUM(" + temp + ") as " + temp + "Sum", this.BaseEntityMap);
}
/// <summary>
/// 统计条数
/// </summary>
/// <returns>统计条数属性</returns>
public Expression Count()
{
string temp = this.PropertyName.Substring(this.PropertyName.IndexOf('.') + 1, this.PropertyName.Length - this.PropertyName.IndexOf('.') - 1);
return new Expression("COUNT(0) as " + temp + "Count", this.BaseEntityMap);
}
/// <summary>
/// 求平均统计
/// </summary>
/// <returns>求平均统计属性</returns>
public Expression Avg()
{
string temp = this.PropertyName.Substring(this.PropertyName.IndexOf('.') + 1, this.PropertyName.Length - this.PropertyName.IndexOf('.') - 1);
return new Expression("AVG(" + temp + ") as " + temp + "Avg", this.BaseEntityMap);
}
/// <summary>
/// 求最大值
/// </summary>
/// <returns>统计求最大值</returns>
public Expression Max()
{
string temp = this.PropertyName.Substring(this.PropertyName.IndexOf('.') + 1, this.PropertyName.Length - this.PropertyName.IndexOf('.') - 1);
return new Expression("MAX(" + temp + ") as " + temp + "Max", this.BaseEntityMap);
}
/// <summary>
/// 求最小值
/// </summary>
/// <returns>统计求最小值</returns>
public Expression Min()
{
string temp = this.PropertyName.Substring(this.PropertyName.IndexOf('.') + 1, this.PropertyName.Length - this.PropertyName.IndexOf('.') - 1);
return new Expression("MIN(" + temp + ") as " + temp + "Min", this.BaseEntityMap);
}
#endregion
}
}