相关疑难解决方法(0)

LINQ成员表达式获取列名

你好,

我正在使用LINQ和EF与C#4.0.我已将基本的ELMAH表拖入EF(已构建并保存了很多次).一切都按照人们的预期运作.

但是试图过于雄心勃勃并需要一些帮助 - 我试图从作为变量传入的表达式中获取Column名称.

我想要的是这个:

传入: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 …
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework linq-expressions

6
推荐指数
1
解决办法
3634
查看次数

标签 统计

c# ×1

entity-framework ×1

linq ×1

linq-expressions ×1