How to refactor Linq Group By and Foreach statement into one Linq Query

c# entity-framework-core linq linq-to-entities

Question

I am having some trouble trying to refactor the LINQ query below into one single query:

// produces 8 results
            var qryResults =
                (from r in dbContext.Routes
                 join br in dbContext.BrokerRoutes on r.RouteId equals br.RouteId
                 join pr in dbContext.RoutePathFilters.Include(x => x.PathFilter) on r.RouteId equals pr.RouteId into paths
                 where br.Broker.ApiKey == apiKey
                 select new RouteTemplate
                    {
                        RouteId = r.RouteId,
                        Version = r.Version.Version,
                        Url = r.Url,
                        IsActive = r.IsActive,
                        Paths = paths.Select(x => x.PathFilter.FilterName).ToList()
                 })
                .AsNoTracking()
                .ToImmutableList();
// takes 8 results and turns that into 2 overall objects.
            var results = new List<RouteTemplate>();
            foreach (var r in qryResults)
            {
                if (results.All(x => x.RouteId != r.RouteId))
                {
                    r.Paths = qryResults
                        .Where(x => x.RouteId == r.RouteId)
                        .Select(x => x.Paths.First()).ToList();
                    results.Add(r);
                }
            }

r is duplicated by each value held in the paths property.

I know that foreach can be combined into the linq query above but i have been trying for a few hours now. So i am interesting know if it possible or not and how can it be done ?

1
0
3/1/2019 12:35:34 PM

Popular Answer

Without the actual database, I am not sure if this will work, but here is my attempt:

var qryResults =
    (from r in dbContext.Routes
     join br in dbContext.BrokerRoutes on r.RouteId equals br.RouteId
     where br.Broker.ApiKey == apiKey
     join pr in dbContext.RoutePathFilters.Include(x => x.PathFilter) on r.RouteId equals pr.RouteId into paths
     group new { r, paths } by r.RouteId into rprg
     let r = rprg.First().r
     select new RouteTemplate {
         RouteId = rprg.Key,
         Version = r.Version.Version,
         Url = r.Url,
         IsActive = r.IsActive,
         Paths = rprg.Select(rpr => rpr.paths.First().PathFilter.FilterName).ToList()
     })
    .AsNoTracking()
    .ToImmutableList();
1
3/1/2019 8:02:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow