实体框架提供列名称作为字符串变量

发布于 2024-12-11 12:44:35 字数 222 浏览 0 评论 0原文

我正在寻找获得类似以下内容的方法:

string _col1 = "first name";
string _name;
var query = from c in ctx.Customers select c;
_name = query.FirstOrDefault().[_name];

据我所知,我只能获得强类型字段名称,但我想将它们作为字符串变量提供。

I'm looking for way to get something like this:

string _col1 = "first name";
string _name;
var query = from c in ctx.Customers select c;
_name = query.FirstOrDefault().[_name];

As far as I can see I can only get strongly typed field names but I would like to provide them as string variables.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

披肩女神 2024-12-18 12:44:35

我不确定 EF 是否为您提供了一种根据属性的字符串名称获取属性值的方法,但您可以使用反射。

string name = typeof(Customer)
    .GetProperty("first name")
    .GetValue(query.First(), null) as string;

我猜您正在处理的 EF 类称为 Customer

I'm not sure if EF provides you with a way to get a property value based on the string name of the property, but you can use reflection.

string name = typeof(Customer)
    .GetProperty("first name")
    .GetValue(query.First(), null) as string;

I'm guessing the EF class you're dealing with is called Customer.

死开点丶别碍眼 2024-12-18 12:44:35

为此,您需要使用反射。如果您尝试按动态选择的列进行过滤,您可以尝试这样的操作:

string propertyName
string keyword

ParameterExpression parameter = Expression.Parameter(typeof(YourType), "x");
Expression property = Expression.Property(parameter, propertyName);
Expression target = Expression.Constant(keyword);
Expression containsMethod = Expression.Call(property, "Contains", null, target);
Expression<Func<YourType, bool>> lambda =
   Expression.Lambda<Func<YourType, bool>>(containsMethod, parameter);

var companies = repository.AsQueryable().Where(lambda);

我您想要做的是选择特定列,然后您可以使用相同的原理来生成兰巴表达式并在选择中使用它(减去条件)

var companies = repository.AsQueryable().Where(whatever).Select(lambda);

You need to use reflection for this. If you are trying to filter by a dynamicly selected column, you can try something like this:

string propertyName
string keyword

ParameterExpression parameter = Expression.Parameter(typeof(YourType), "x");
Expression property = Expression.Property(parameter, propertyName);
Expression target = Expression.Constant(keyword);
Expression containsMethod = Expression.Call(property, "Contains", null, target);
Expression<Func<YourType, bool>> lambda =
   Expression.Lambda<Func<YourType, bool>>(containsMethod, parameter);

var companies = repository.AsQueryable().Where(lambda);

I what you are trying to do is selecting a particular column, then you can use the same principle for generating the lamba expression and using it in the select (minus the condition)

var companies = repository.AsQueryable().Where(whatever).Select(lambda);
从此见与不见 2024-12-18 12:44:35

由于某种原因,它对我不起作用。

这是我类似的工作解决方案:

string name = null;

// Select the PropertyInfo of the column.
PropertyInfo propertyInfo = query.First().GetType().GetProperty("first name");

if (propertyInfo != null)
{
  try
  {
    // Select the content of the column.
    name = pi.GetValue(query.First(), null).ToString();
  }
  catch (Exception)
  { 
    // Continue with null-string.
  }

}

For some kind of reason it won't work for me.

That's my similar working solution:

string name = null;

// Select the PropertyInfo of the column.
PropertyInfo propertyInfo = query.First().GetType().GetProperty("first name");

if (propertyInfo != null)
{
  try
  {
    // Select the content of the column.
    name = pi.GetValue(query.First(), null).ToString();
  }
  catch (Exception)
  { 
    // Continue with null-string.
  }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文