将几个组组合到View

时间:2013-12-31 09:20:20

标签: asp.net-mvc linq entity-framework viewmodel grouping

我如何构建此查询选择的ViewModel?

var result =
from company in repository.CompanyRepository.Get()
join account in repository.AccountRepository.Get() on company.Uid equals account.UserId
from notice in company.Notices
join request in
    repository.RequestRepository.Get() on
    notice.SubcategoryId equals request.Subcategoryid
group new {notice, request} by new {company, account}
into g
select new {g.Key, value = g};

我正在玩不同类型,但我无法理解。

IQueryable<IGrouping<IGrouping<Company, Account>, IGrouping<Notice, Request>>> testGroup;

1 个答案:

答案 0 :(得分:1)

所以你可以致电ToList来执行它并放弃IGgrouping也可以稍微改变一下select clause

select new ViewModelClass{Company=g.Key.Company, Account=g.Key.Account, Notices=g.Select(n=>n.notice), Requests=g.Select(r=>r.request)}

viewModelClass这样的事情

class ViewModelClass{
    public Company Company { get; set; };
    public Account Account { get; set; };
    public IEnumerable<Notice> Notices { get; set; };
    public IEnumerable<Request> Requests { get; set; };
}

您可能需要使用IQueryable代替IEnumerable