c#linq group by into view models

时间:2015-06-29 20:16:01

标签: c# linq entity-framework

我的矩阵表看起来像这样

enter image description here

使用linq我希望结果绑定到下面的视图模型。 matricesView将是Y坐标,并且具有(矩阵)所有X坐标值的列表。

public class MatricesView
{
    public List<MatrixView> Matrix { get; set; }
    public MatricesView()
    {
        Matrix = new List<MatrixView>();
    }
}
public class MatrixView
{
    public string Value { get; set;}
}

我目前正在查询,然后循环创建我的视图模型。

int previousY = 0;
            MatricesView matrice = new MatricesView();
            foreach(var matrix in table.Matrices)
            {
                if (previousY != matrix.Y)
                {
                    matrice = new MatricesView();
                    Matrices.Add(matrice);
                    previousY = matrix.Y;
                }
                matrice.Matrix.Add(new MatrixView() { Value = matrix.Value == "****" ? " " : matrix.Value});
            }

在查询我的数据库时,是否可以直接使用linq执行此逻辑:

    using (var context = new ApplicationDbContext())
    {
        return context.Tables
                .Where(predicate)
                .Select(t => new TableViewModel()
                {
                    Name = t.Name,
                    Question = t.Question,
                    Base = t.Base,
                    Categories = t.Categories.Select(category => new CategoryView() { Name = category.Name, ColSpan = category.ColSpan }).ToList(),
                    Demographics = t.Demographics.Select(demographic => new DemographicView() { Name = demographic.Value }).ToList(),
                    Trailers = t.Trailers.Select(trailer => new TrailerView() { Name = trailer.Value }).ToList(),
                    Matrices = .....?
                }).ToList();
    }

编辑:

解决方案是:

    using (var context = new ApplicationDbContext())
    {
        return context.Tables
                .Where(predicate)
                .Select(t => new TableViewModel()
                {
                    Name = t.Name,
                    Question = t.Question,
                    Base = t.Base,
                    Categories = t.Categories.Select(category => new CategoryView() { Name = category.Name, ColSpan = category.ColSpan }).ToList(),
                    Demographics = t.Demographics.Select(demographic => new DemographicView() { Name = demographic.Value }).ToList(),
                    Trailers = t.Trailers.Select(trailer => new TrailerView() { Name = trailer.Value }).ToList(),
                    Matrices = t.Matrices.GroupBy(x => x.Y).Select(x => new MatricesView { Matrix = x.Select(y => new MatrixView(){ Value = y.Value}).ToList() }).ToList()
                }).ToList();
    }

1 个答案:

答案 0 :(得分:1)

你应该可以做一个小组。最终结果看起来像这样:

return context.Tables
    .Where(predicate)
    .GroupBy(x => x.Y)
    .Select(t => new TableViewModel()
    {
        Name = t.FirstOrDefault().Name,
        Question = t.FirstOrDefault().Question,
        Base = t.FirstOrDefault().Base,
        Categories = t.FirstOrDefault().Categories.Select(category => new CategoryView() { Name = category.Name, ColSpan = category.ColSpan }).ToList(),
        Demographics = t.FirstOrDefault().Demographics.Select(demographic => new DemographicView() { Name = demographic.Value }).ToList(),
        Trailers = t.FirstOrDefault().Trailers.Select(trailer => new TrailerView() { Name = trailer.Value }).ToList(),
        Matrices = new MatrixView() 
        { 
            Matrix = t.Select(x => new MatrixView { Value = x.Value == "****" ? " " : x.Value }).ToList() 
        }
    }).ToList();

编辑:为了完整起见,迈克在最终结果中使用了这些内容:

...
Matrices = t.Matrices.GroupBy(x => x.Y).Select(x => new MatricesView 
{ 
    Matrix = x.Select(y => new MatrixView(){ Value = y.Value}).ToList() 
}).ToList()