gpt4 book ai didi

c# - IEnumerable 返回优先

转载 作者:行者123 更新时间:2023-11-30 14:34:38 26 4
gpt4 key购买 nike

我有以下代码:

    public IEnumerable<Report> GetReport(int reportId)
{
var dbReport = dbContext.ReportsTbl.Where(w =>w.ID == reportId);
return dbReport;

}

为了获得第一我喜欢做什么

如果我这样做:

    public IEnumerable<Report> GetReport(int reportId)
{
var dbReport = dbContext.ReportsTbl.First(w =>w.ID == reportId);
return dbReport;
}

我如何让它执行 First()。它提示它是 IEnumerable。

最佳答案

您需要更改方法签名以仅返回单个对象而不是集合:

public Report GetReport(int reportId)
{
var dbReport = dbContext.ReportsTbl.First(w =>w.ID == reportId);
return dbReport;
}

如果出于某种原因您确实想要一个仅包含第一个元素的集合,您可以使用 .Take(1) 而不是 First

关于c# - IEnumerable 返回优先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13673350/

26 4 0