简体   繁体   中英

Is there any way to reduce duplication in these two linq queries

Building a bunch of reports, have to do the same thing over and over with different fields

    public List<ReportSummary> ListProducer()
    {
        return (from p in Context.stdReports                    
                group p by new { p.txt_company, p.int_agencyId }
                    into g
                    select new ReportSummary
                    {
                        PKi = g.Key.int_agencyId,
                        Name = g.Key.txt_company,
                        Sum = g.Sum(foo => foo.lng_premium),
                        Count = g.Count()
                    }).OrderBy(q => q.Name).ToList();
    }

    public List<ReportSummary> ListCarrier()
    {
        return (from p in Context.stdReports
                group p by new { p.txt_carrier, p.int_carrierId }
                    into g
                    select new ReportSummary
                    {
                        PKi = g.Key.int_carrierId,
                        Name = g.Key.txt_carrier,
                        Sum = g.Sum(foo => foo.lng_premium),
                        Count = g.Count()
                    }).OrderBy(q => q.Name).ToList();
    }

My Mind is drawing a blank on how i might be able to bring these two together.

It looks like the only thing that changes are the names of the grouping parameters. Could you write a wrapper function that accepts lambdas specifying the grouping parameters? Or even a wrapper function that accepts two strings and then builds raw T-SQL, instead of using LINQ?

Or, and I don't know if this would compile, can you alias the fields in the group statement so that the grouping construct can always be referenced the same way, such as g.Key.id1 and g.Key.id2 ? You could then pass the grouping construct into the ReportSummary constructor and do the left-hand/right-hand assignment in one place. (You'd need to pass it as dynamic though, since its an anonymous object at the call site)

You could do something like this:

public List<ReportSummary> GetList(Func<Record, Tuple<string, int>> fieldSelector)
{
    return (from p in Context.stdReports                    
        group p by fieldSelector(p)
            into g
            select new ReportSummary
            {
                PKi = g.Key.Item2
                Name = g.Key.Item1,
                Sum = g.Sum(foo => foo.lng_premium),
                Count = g.Count()
            }).OrderBy(q => q.Name).ToList();
}

And then you could call it like this:

var summary = GetList(rec => Tuple.Create(rec.txt_company, rec.int_agencyId));

or:

var summary = GetList(rec => Tuple.Create(rec.txt_carrier, rec.int_carrierId));

Of course, you'll want to replace Record with whatever type Context.stdReports is actually returning.

I haven't checked to see if that will compile, but you get the idea.

Since all that changes between the two queries is the group key, parameterize it. Since it's a composite key (has more than one value within), you'll need to create a simple class which can hold those values (with generic names).

In this case, to parameterize it, make the key selector a parameter to your function. It would have to be an expression and the method syntax to get this to work. You could then generalize it into a function:

public class GroupKey
{
    public int Id { get; set; }
    public string Name { get; set; }
}

private IQueryable<ReportSummary> GetReport(
    Expression<Func<stdReport, GroupKey>> groupKeySelector)
{
    return Context.stdReports
        .GroupBy(groupKeySelector)
        .Select(g => new ReportSummary
        {
            PKi = g.Key.Id,
            Name = g.Key.Name,
            Sum = g.Sum(report => report.lng_premium),
            Count = g.Count(),
        })
        .OrderBy(summary => summary.Name);
}

Then just make use of this function in your queries using the appropriate key selectors.

public List<ReportSummary> ListProducer()
{
    return GetReport(r =>
        new GroupKey
        {
            Id = r.int_agencyId,
            Name = r.txt_company,
        })
        .ToList();
}

public List<ReportSummary> ListCarrier()
{
    return GetReport(r =>
        new GroupKey
        {
            Id = r.int_carrierId,
            Name = r.txt_carrier,
        })
        .ToList();
}

I don't know what types you have mapped for your entities so I made some assumptions. Use whatever is appropriate in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM