你好,
我正在使用C# 4.0的LINQ和EF。
我已经将基本的ELMAH表拖入EF(构建并保存了很多次)。
一切都按预期工作。
但是我试图过于雄心勃勃,需要一点帮助 - 我正在尝试从作为变量传递的表达式中获取列名。
我想要的是:
传入:x=>x.ErrorId
得到:"ErrorId"
public void GetColumnName(Expression<Func<T, object>> property)
{
// The parameter passed in x=>x.Message
// Message works fine (probably because its a simple string) using:
string columnName = (property.Body as MemberExpression).Member.Name;
// But if I attempt to use the Guid or the date field then it
// is passed in as x => Convert(x.TimeUtc)
// As a result the above code generates a NullReference exception
// i.e. {"Object reference not set to an instance of an object."}
// What is the correct code here to extract the column name generically?
// Ideally in a way that won't bite me again in the future.
}
感谢您的帮助! 丹。
x.ColumnName
ж›өе¤Қжқ‚зљ„иҰЁиңңеәЏдёзҰ®е®ље€—еђҚеђ—пәџ - user7116property.Body as MemberExpression
放入监视窗口,一旦触发ErrorId
,您可能会看到如何提取它。 - oleksii