c# - 如何使用 Linq 选择列名

标签 c# entity-framework linq

<分区>

我陷入了逻辑需要改进的境地,而我正在尝试这样做。 情况是我们都知道如何像这样更新列的具体细节。

Person result = (from p in Context.Persons
          where p.person_id == 5
          select p).SingleOrDefault();

result.is_default = false;

Context.SaveChanges();

好的,一切顺利!

现在假设我在 session is_default 中有一个值,即表的列名。

如何使用该值更新列:

update table name set "Session value" = true 

这里的 session 值是is_default,它是表的列名。这可能使用 EF , LINQ 。

最佳答案

您可以为此使用反射的概念,如下所示

Person result = (from p in Context.Persons
          where p.person_id == 5
          select p).SingleOrDefault();

PropertyInfo propertyInfo = result.GetType().GetProperty("Session value");
propertyInfo.SetValue(result , true, null);

关于c# - 如何使用 Linq 选择列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52218295/

相关文章:

c# - 将轻量级 Web 服务器嵌入到 .net 应用程序(node.js)中?

c# - SOAP 请求因 MEX 绑定(bind)而失败

.net - 如何为 Entity Framework 创建三层解决方案

mysql - 如何先使用MySql和Entity Framework 4.1代码

c# - ASP.NET MVC 5 标识中的空引用

c# - 如果使用 SingleOrDefault() 并在数字列表中搜索不在列表中的数字,如何返回 null?

c# - DateTime 编辑器模板未以正确格式呈现日期

c# - 解释 Linq Microsoft Select - 索引 [示例]

c# - 在 LINQ 中获取动态 OrderBy

c# - 使用枚举是列出不同 "Types of Car"的最佳方式吗