c# - 从方法返回 LINQ 数据库查询

标签 c# sql linq

大家好,我要在多个地方执行此查询。我希望能够调用一个返回查询的方法,而不是一遍又一遍地重新键入查询。我不确定将什么作为方法的返回类型,或者是否可以这样做。我使用查询编写信息的 csv 文件,并使用查询将项目添加到绑定(bind)到 ListView 的可观察集合。

using (ProjectTrackingDBEntities context = new ProjectTrackingDBEntities())
{
    var result = context.TimeEntries.Where(Entry => Entry.Date >= FilterProjectAfterDate
                                && Entry.Date <= FilterProjectBeforerDate
                                && (FilterProjectName != null ? Entry.ProjectName.Contains(FilterProjectName) : true))
        .GroupBy(m => new { m.ProjectName, m.Phase })
        .Join(context.Projects, m => new { m.Key.ProjectName, m.Key.Phase }, w => new { w.ProjectName, w.Phase }, (m, w) => new { te = m, proj = w })
        .Select(m => new
        {
            Name = m.te.Key.ProjectName,
            Phase = m.te.Key.Phase,
            TimeWorked = m.te.Sum(w => w.TimeWorked),
            ProposedCompletionDate = m.proj.ProposedCompletionDate,
            ActualCompletionDate = m.proj.ActualCompletionDate,
            Active = m.proj.Active,
            StartDate = m.proj.StartDate,
            Description = m.proj.Description,
            EstimatedHours = m.proj.EstimatedHours
        });
}

我现在可以通过重新键入查询并对数据执行后续的 foreach() 循环来同时执行这两项操作。我宁愿能够做这样的事情:

var ReturnedQuery = GetProjectsQuery();
foreach(var item in ReturnedQuery)
{
    //do stuff
}

如有任何帮助,我们将不胜感激。

最佳答案

您想返回 IQueryable<T>用一个已知的模型来代表你要返回的是什么。您不应返回匿名类型。您还想传入 DbContext所以它可以由调用者而不是在方法中处理,否则你会收到一个异常 DbContext已被处置。

例如:

public IQueryable<ProjectModel> GetProjectQuery(ProjectTrackingDBEntities context) {

    return context.TimeEntries.Where(Entry => Entry.Date >= FilterProjectAfterDate
                                && Entry.Date <= FilterProjectBeforerDate
                                && (FilterProjectName != null ? Entry.ProjectName.Contains(FilterProjectName) : true))
        .GroupBy(m => new { m.ProjectName, m.Phase })
        .Join(context.Projects, m => new { m.Key.ProjectName, m.Key.Phase }, w => new { w.ProjectName, w.Phase }, (m, w) => new { te = m, proj = w })
        .Select(m => new ProjectModel
        {
            Name = m.te.Key.ProjectName,
            Phase = m.te.Key.Phase,
            TimeWorked = m.te.Sum(w => w.TimeWorked),
            ProposedCompletionDate = m.proj.ProposedCompletionDate,
            ActualCompletionDate = m.proj.ActualCompletionDate,
            Active = m.proj.Active,
            StartDate = m.proj.StartDate,
            Description = m.proj.Description,
            EstimatedHours = m.proj.EstimatedHours
        });
}

ProjectModel.cs

public class ProjectModel {
    public string Name {get;set;}
    public string Phase {get;set;}
    // rest of properties
}

调用代码

using (ProjectTrackingDBEntities context = new ProjectTrackingDBEntities())
{   
    var ReturnedQuery = GetProjectsQuery(context);
    foreach(var item in ReturnedQuery)
    {
        //do stuff
    }
}

关于c# - 从方法返回 LINQ 数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49158622/

相关文章:

Mysql - 如何退出/退出存储过程

sql - 如何根据 PostgreSQL 中的行差异有条件地合并行?

c# - View 中的 Rowspan (mvc4)

linq - 动态地将类型传递给方法<T>

c# - Entity Framework DBContext 全局缓存?

c# - xml注释是否在 Release模式下持续存在?

c# - 动态转换为通用接口(interface)

c# - 如何在慢速互联网连接上测试程序?

php - 多个连接在 Yii2 的 where 条件下表现不同

c# - 从 Azure 表中删除数据