日期:2014-05-17  浏览次数:20837 次

高难度,请教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()