gpt4 book ai didi

c# - Linq - 选择一个特定的列

转载 作者:太空狗 更新时间:2023-10-30 00:51:35 26 4
gpt4 key购买 nike

在表中,我有 5 列:- 姓名、年龄、出生日期、司法管辖区、入职日期。

string column = 'dob'; // This is dynamic

var data = ctx.tblTable
.Where(e => e.Id == Id && e.Name == name)
.Select(e => column).SingleOrDefault();

不幸的是,这不起作用。如何在 linq 中选择特定的列。

最佳答案

string column = 'dob'; // This is dynamic

var data = ctx.tblTable
.Where(e => e.Id == Id && e.Name == name)
.ToList()
.Select(e => GetPropValue(e, column))
.FirstOrDefault();


public object GetPropValue(object obj, string propName)
{
return obj.GetType().GetProperty(propName).GetValue(obj, null);
}

关于c# - Linq - 选择一个特定的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26211628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com