如何从模型.Net的列返回数据

问题描述 投票:0回答:1

我想获取模型对象(自然)的“类型”列的数据

  public async Task<IEnumerable<Nature>> FindAll()
    {
        using DBModelContext context = CreateNewContext();
        IEnumerable<Nature> nature = await context.Nature.Where(_ => _.Type); 
            return nature; 
    }

“哪里”没有提供给我。

c# asp.net .net database
1个回答
0
投票

我对您问题的理解是,您希望返回

Type
表的
Nature
列中保存的所有值。

由于问题中没有类型信息,我假设

Nature.Type
string
属性。

Queryable.Where()
采用谓词参数来过滤表查询的结果。看起来这不是你的意图。相反,请尝试使用
Queryable.Select()
:

IEnumerable<string> nature = await context.Nature.Select(nature => nature.Type);

然后,您需要将方法的返回类型从

IEnumerable<Nature>
更改为
IEnumerable<string>
,因为您只返回
Type
列的值,而不是整个
Nature
对象。

© www.soinside.com 2019 - 2024. All rights reserved.