【问题标题】:How to retrieve this list of requests with the status of each request?如何使用每个请求的状态检索此请求列表?
【发布时间】:2016-04-17 06:38:44
【问题描述】:

我是一名新的 ASP.NET Webforms 开发人员。我现在正在学习如何使用实体框架和存储库模式。我现在正在努力的是如何从 TRequest 表中检索请求列表以及每个请求的批准状态。这是我的数据库架构:

TRequest: Id, Title, Reason
TApproval: Id, RequestId, StatusId
TStatus: Id, Title

我想要做的是检索请求列表,包括每个请求的状态。

在我的存储库模式中,我有以下代码:

public IEnumerable<TRequest> GetRequest()
{
    return context.TRequest.ToList();
}

public IEnumerable<TRequest> GetRequestByStatus(int statusId)
{
    return GetRequests().Where(r => r.TApproval.TStatus.Id).ToList();   //I got an error here
}

为什么我在第二种方法 GetRequestByStatus 中出现错误? 我无法说出 r.TApproval.TStatus.Id 并且我不知道为什么。

【问题讨论】:

    标签: c# asp.net entity-framework-6 repository-pattern


    【解决方案1】:

    为什么我在第二种方法 GetRequestByStatus 中出现错误?我无法 > 说 r.TApproval.TStatus.Id 并且我不知道为什么。

    Where 方法需要一个谓词,而您没有在那里传递一个谓词。更正式地说,正如MSDN 中所述。

    where 子句在查询表达式中用于指定哪个 数据源中的元素将在查询中返回 表达。 它将布尔条件(谓词)应用于每个源 元素(由范围变量引用)并返回那些 指定条件为真。单个查询表达式可能包含 多个 where 子句和单个子句可能包含多个 谓词子表达式。

    【讨论】:

    • 感谢您尝试帮助我。我知道这一点,但我真的不知道如何解决我上面解释的问题。
    • @TechLover 欢迎您。所以,这里有些奇怪。正如我所见,TRequest 没有对TApproval 的任何引用。话虽如此,我认为通过查询TRequest 表来实现您想要的并不可行。这是奇怪的想法。另一方面,我可以假设您可以通过查询TApproval 表并在结果中包含相应的TRequest 来获得您想要的结果。我认为像context.TApproval.Inlcude(appr=&gt;appr.TRequest) 这样的东西可能对您的情况有所帮助。
    【解决方案2】:

    如果您尝试按状态 ID 进行过滤,则需要将行更改为

    return GetRequests().Where(r => r.TApproval.TStatus.Id == statusId).ToList();
    

    【讨论】:

    • 感谢您的帮助。问题是我无法添加TStatus。我在这行代码下面遇到错误,我不知道为什么。
    猜你喜欢
    • 2020-10-07
    • 1970-01-01
    • 2014-12-30
    • 2015-07-19
    • 2023-03-12
    • 2016-02-29
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多