LINQ按条款动态分组

时间:2018-10-18 13:35:04

标签: linq group-by

var query = trafficHandler.portList
            .Join(
            trafficHandler.timeList,
            p => p.PortId,
            t => t.PortId,
            (p, t) => new { Port = p, Time = t })
            .Join(
            trafficHandler.trafficList,
            pt => pt.Time.TimeId,
            tr => tr.TimeId,
            (pt, tr) => new { Port = pt.Port, Time = pt.Time, Traffic = tr })
            .Select(x => new
            {
                x.Port.PortNumber,
                x.Time.TimeNumber,
                x.Time.DayNumber,
                x.Traffic.Action,
                x.Traffic.User
            })
            .Where(x => allowedPort.Contains(x.PortNumber) && allowedDay.Contains(x.DayNumber) && allowedTime.Contains(x.TimeNumber) && allowedUser.Contains(x.User) && allowedAction.Contains(x.Action))
            .GroupBy(y => new { y.DayNumber, y.TimeNumber, y.User })
            .Where(z => z.Count() >= baseline)
            .OrderBy(g => g.Key.DayNumber)
            .ThenBy(g => g.Key.TimeNumber)
            .ThenBy(g => g.Key.User);

我如何动态地允许某些列可以分组?就像有时我需要y.Action也要包括在Group By子句中一样,我不想重写每个LINQ查询,这将有很多冗余功能。阅读有关Stack Overflow的其他一些类似问题,似乎您将需要动态LINQ库,是否还有其他解决方法?

0 个答案:

没有答案