Daryl Leak
Daryl Leak

Reputation: 80

Adding two Lambda/Linq Queries together

I have the following code which is getting me one set of results and then another (resultsSetup) which is exactly the same based on if a condition is met.

// use Linq to query the downtime data
var results = from t in this.MachineEvent.Transactions.OfType<Logic.DowntimeScrapTransaction>()
                where t.TransactionType == Logic.Enums.MachineEventTransactionType.DowntimeScrapTransaction
                group t by t.Reason.Group into grps
                let totalDowntime = grps.Sum(g => g.DowntimeDuration.TotalMinutes)
                orderby totalDowntime descending
                select new { Key = grps.Key, values = grps, TotalDownTime = grps.Sum(g => g.DowntimeDuration.TotalMinutes) };


if (this.MachineEvent.MachineState == Logic.Enums.MachineState.Production)
{
    var resultsSetup = from t in this.MachineEvent.GetSetupMachineEvent().Transactions.OfType<Logic.DowntimeScrapTransaction>()
                        where t.TransactionType == Logic.Enums.MachineEventTransactionType.DowntimeScrapTransaction
                        group t by t.Reason.Group into grps
                        let totalDowntime = grps.Sum(g => g.DowntimeDuration.TotalMinutes)
                        orderby totalDowntime descending
                        select new { Key = grps.Key, values = grps, TotalDownTime = grps.Sum(g => g.DowntimeDuration.TotalMinutes) };

    results.Concat(resultsSetup);
    results.Union(resultsSetup);
}

What I am trying to do is simply combine these two results together. As you can see I've tried the Concat and Union method which I am having no success with either.

Upvotes: 2

Views: 834

Answers (3)

Davecz
Davecz

Reputation: 1221

Try this...

results = results.Union(resultsSetup);

Upvotes: 1

Sean
Sean

Reputation: 62542

I think you mean to say:

results = results.Concat(resultsSetup);

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727067

As you can see I've tried the Concat and Union method which I am having no success with either.

This is because you've ignored their return value. You should have done either

results = results.Concat(resultsSetup);

if you want to append all items of resultsSetup to results, or

results = results.Union(resultsSetup);

if you would like to obtain a union (i.e. remove all duplicates).

Upvotes: 3

Related Questions