Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 筛选“基本查询”以获得略有不同的结果_C#_Sql Server_Entity Framework - Fatal编程技术网

C# 筛选“基本查询”以获得略有不同的结果

C# 筛选“基本查询”以获得略有不同的结果,c#,sql-server,entity-framework,C#,Sql Server,Entity Framework,我正在尝试使用实体框架查询数据库,需要在同一组表上进行几个稍微不同的查询。我需要添加大量的导航属性,在我看来,我应该能够定义基本查询,即具有所有导航属性的查询,然后根据需要进一步筛选并执行查询,这是合乎逻辑的 一些代码可能有助于进一步解释。这就是我所说的基本查询 private static IEnumerable<HelpdeskTicket> GetAll() { IEnumerable<HelpdeskTicket> Tickets; using

我正在尝试使用实体框架查询数据库,需要在同一组表上进行几个稍微不同的查询。我需要添加大量的导航属性,在我看来,我应该能够定义基本查询,即具有所有导航属性的查询,然后根据需要进一步筛选并执行查询,这是合乎逻辑的

一些代码可能有助于进一步解释。这就是我所说的基本查询

private static IEnumerable<HelpdeskTicket> GetAll()
{
    IEnumerable<HelpdeskTicket> Tickets;

    using (ItManagement_Entities db = new ItManagement_Entities())
    {
        Tickets = db.HelpdeskTickets.Include("CreatedByPerson")
                                    .Include("HelpdeskCategory")
                                    .Include("HelpdeskPriority")
                                    .Include("HelpdeskStatus");
    }

    return Tickets;
}
当我尝试此操作时,抛出System.InvalidOperationException异常,抱怨操作无法完成,因为DbContext已被释放,这很有意义,因为我的查询在GetAll方法中处于不同的上下文中


问题是,我该如何做我想做的事情?

您可以尝试类似的方法,其中每个特定的方法调用一些基本的、私有的方法来完成常见的工作,然后每个方法添加其特定的查询位。类似的东西可能会作为一个起点派上用场:

// Here you define common parts applicable to all methods, or at least shared among some of them
private static IQueryable<HelpdeskTicket> BuildBaseQuery(this IQueryable<HelpdeskTicket> query)
{
    return query.Include("CreatedByPerson")
                .Include("HelpdeskCategory")
                .Include("HelpdeskPriority")
                .Include("HelpdeskStatus");
}

// Here are the particular methods, they create a query, call helper methods for the common bits and add their specifics
public static List<HelpdeskTicketModel> GetAllTickets()
{
    List<HelpdeskTicketModel> Tickets = new List<HelpdeskTicketModel>();

    using (ItManagement_Entities db = new ItManagement_Entities())
    {
        Tickets = db.HelpdeskTickets.BuildBaseQuery()
                                    .OrderByDescending(t => t.TicketId)
                                    .ToList()
                                    .ForEach(t => Tickets.Add(HelpdeskTicketModel.Map(t)));
    }

    return Tickets;
}

您可以始终将dbcontext传递到GetAllTickets方法中。
// Here you define common parts applicable to all methods, or at least shared among some of them
private static IQueryable<HelpdeskTicket> BuildBaseQuery(this IQueryable<HelpdeskTicket> query)
{
    return query.Include("CreatedByPerson")
                .Include("HelpdeskCategory")
                .Include("HelpdeskPriority")
                .Include("HelpdeskStatus");
}

// Here are the particular methods, they create a query, call helper methods for the common bits and add their specifics
public static List<HelpdeskTicketModel> GetAllTickets()
{
    List<HelpdeskTicketModel> Tickets = new List<HelpdeskTicketModel>();

    using (ItManagement_Entities db = new ItManagement_Entities())
    {
        Tickets = db.HelpdeskTickets.BuildBaseQuery()
                                    .OrderByDescending(t => t.TicketId)
                                    .ToList()
                                    .ForEach(t => Tickets.Add(HelpdeskTicketModel.Map(t)));
    }

    return Tickets;
}