高难度,请教lamda表达式 如何实现一个方法,参数是某个类中的多个成员,如://Expression是表达式,表达式格式为类成员,如test<类>(n => n.成员1,n.成员2)void test<T>(Expression...) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 C#中,函数只能有唯一的返回值。只能写成test<类>(n => new { n.成员1, n.成员2 }) 如果你是封装了LINQ2SQL, 为了动态查询,可以参数 规约模式,或者自己写个多条件查询的构造类http://linqspecs.codeplex.com/ 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; } }} 谢谢大家BuildQueryString<Book>(b => b.Id, b => b.Name);//b => b.Id, b => b.Name参数形式有没有办法再简化? 为什么checkbox要点很多下才能选中? C# 修改组策略里面的软件限制策略 怎么修改 ???asp.net2.0 DataGrid的层次显示 出严重错误了·高手帮忙呀 SendKeys.SendWait("^C") 请教关于图片处理的问题? 求C#基于Socket实现多线程传输文件案例 如何制作托盘程序?socket程序服务器端在关闭窗口后,进程仍然存在? 我引用了Windows Media Player组件了工具栏里没有出现Media Player啊??? 什么是钩子? 如何编写“设置快捷键”的功能? 关于winform非正常关闭(结束进程)时,更改数据库用户状态
只能写成
test<类>(n => new { n.成员1, n.成员2 })
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; }
}
}