使用字符串代替属性名称(LINQ) [英] Using string in place of property name (LINQ)

查看:61
本文介绍了使用字符串代替属性名称(LINQ)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够使用linq从表中获取值.

I have been able to get the values from tables using linq.

var q=(from app in context.Applicant 
        where app.ApplicantName=="")

现在我想要的是这个

 var q=(from app in context.Applicant 
        where app.stringIhave =="")  // so instead of column name I have string which has same name as column.

是否可以在Select中指定字符串,因为这不确定我会在每种情况下得到什么,因此我一直需要不同的数据.

是否可以这样做? 如果否,那么我会找出其他的东西. 更新 我有一个GlobalString,其中包含一个表的列名称. 因此,当我查询该表时,我仅从字符串中指定要获取的列值:

Is it possible to do so? If no, then I will figure out something else. Update I have a GlobalString, which holds the column name of a table. So when I query that table, I only specify from string which column value I want to get:

var q=(from app in context.Applicants
       where app.ID==1013
       select GlobalString //which is specifying that I want to get value from which column, as column name is not fixed.
 //where GlobalString can have values like: app.FirstName..app.LastName etc

更新1:

var q = context.Applicants.Select("new(it.ApplicantFirstName as FirstName, it.ApplicantLastName as LastName)");

错误消息:

The query syntax is not valid. Near keyword 'AS'

推荐答案

您可以使用动态Linq (可从NuGet获得):

You can use Dynamic Linq (available from NuGet) for that:

   var q = context.Applicant.Where(app.stringIhave + " = @0", "");

这篇关于使用字符串代替属性名称(LINQ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆