在C#中,我将使用lambda表达式,我有这样的代码
var item = dbContext.Products.ToList();
我如何获得Product表的属性。
答案 0 :(得分:2)
试试这个
var item = dbContext.Products.FirstOrDefault().Name;
答案 1 :(得分:1)
通常使用Lamba表达式,您可以访问和读取“list”或本例中IQueryable对象的信息。
使用您的代码,您可以使用以下内容访问对象:
var item = dbContext.Products.FirstOrDefault();
// item may be null if products table is empty
if (item != null)
{
// now you can access at object properties (example)
var data = item.PropertyData;
}
您的问题可能会打开其他方式,包括在没有众所周知的类定义的情况下探索对象的反射...
答案 2 :(得分:1)
如果你想获得具有lambda表达式的每个产品的属性,那么你在查询时应该创建一个像x => x.Prop
这样的lambda表达式
if (dbContext.Products != null){
var list = dbContext.Products.ToList();
var query = list.Select(x => x.Prop //your property will appear here...
}