从Method返回LINQ数据库查询

时间:2018-03-07 18:09:50

标签: c# sql linq

大家好我有这个查询我在多个地方表演。我希望能够调用返回查询的方法,而不是一遍又一遍地重新输入查询。我不确定该作为该方法的返回类型放置什么,或者甚至可以这样做。我使用查询来编写信息的csv文件,并使用查询将项目添加到绑定到列表视图的可观察集合中。

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
}

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:4)

您希望使用已知模型返回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
    }
}

答案 1 :(得分:0)

很容易返回枚举器,但不幸的是,您无法为匿名类型返回枚举器。可能最简单的前进方法是将整数行对象返回枚举器,如下所示:

public IEnumerable<TimeEntries> GetTimeEntries()
{
    using (ProjectTrackingDBEntities context = new ProjectTrackingDBEntities())
    {
        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 }
            );
    }
)

}

并像这样使用它:

var query = GetTimeEntries();
foreach (var row in query.Select( m => new { Name = row.te.Key.ProjectName })
{
    Console.WriteLine(row.Name);
}