读书人

高难度请问lamda表达式

发布时间: 2012-10-16 09:57:37 作者: rapoo

高难度,请教lamda表达式
如何实现一个方法,参数是某个类中的多个成员,如:

C# code
//Expression是表达式,表达式格式为类成员,如test<类>(n => n.成员1,n.成员2)void test<T>(Expression...)


[解决办法]
说清楚点,比如接收什么参数,希望得到什么结果
[解决办法]
C#中,函数只能有唯一的返回值。
只能写成
test<类>(n => new { n.成员1, n.成员2 })

[解决办法]
是为了动态查询吗
[解决办法]
如果你是封装了LINQ2SQL, 为了动态查询,

可以参数 规约模式,或者自己写个多条件查询的构造类

http://linqspecs.codeplex.com/
[解决办法]
探讨

如果你是封装了LINQ2SQL, 为了动态查询,

可以参数 规约模式,或者自己写个多条件查询的构造类

http://linqspecs.codeplex.com/

[解决办法]
C# code
namespace GetFields{    class Program    {        static void Main(string[] args)        {            string cmd = BuildQueryString<Book>(b => b.Id, b => b.Name);            Console.WriteLine(cmd);            Console.Read();        }        public static string BuildQueryString<T>(params Expression<Func<T, object>>[] expressions)        {            string selectCommandText = BuildNonConditionQueryString(typeof(T));            List<String> fields = new List<string>();            foreach (var exp in expressions)            {                fields.Add(GetFieldName<T>(exp));            }            if (fields.Count > 0)            {                string temp = String.Join(",", fields);                selectCommandText = selectCommandText.Replace("*", temp);            }            return selectCommandText;        }        public static string GetFieldName<T>(Expression<Func<T, object>> expression)        {            MemberExpression memberExpression = null;            if (expression.Body.NodeType == ExpressionType.Convert)            {                memberExpression = (MemberExpression)((UnaryExpression)expression.Body).Operand;            }            else if (expression.Body.NodeType == ExpressionType.MemberAccess)            {                memberExpression = (MemberExpression)expression.Body;            }            if (null == memberExpression)            {                throw new Exception("....");            }            FieldNameAttribute fa = (FieldNameAttribute)Attribute.GetCustomAttribute(memberExpression.Member, typeof(FieldNameAttribute));            return fa.Name;        }        public static string BuildNonConditionQueryString(Type type)        {            TableNameAttribute ta = (TableNameAttribute)Attribute.GetCustomAttribute(type, typeof(TableNameAttribute));            if (null != ta)            {                StringBuilder sb = new StringBuilder();                sb.AppendFormat(" select * from {0} ", ta.Name);                return sb.ToString();            }            else            {                throw new Exception(".....");            }        }    }    [TableNameAttribute(Name = "tb_book")]    public class Book    {        [FieldName(Name = "id", Key = true)]        public string Id { get; set; }        [FieldName(Name = "name")]        public string Name { get; set; }        [FieldName(Name = "author_id")]        public string AuthorId { get; set; }    }    public class TableNameAttribute : Attribute    {        public string Name { get; set; }    }    public class FieldNameAttribute : Attribute    {        public FieldNameAttribute()        {            this.Key = false;        }        public string Name { get; set; }        public bool Key { get; set; }    }} 

读书人网 >C#

热点推荐