2

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.

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
Daryl Leak
  • 80
  • 9
  • Does `Union` work on anonymous types? – germi Dec 20 '13 at 11:24
  • 1
    You actually have to store the *result* of `Concat` or `Union` in a variable. They both return a new query, they don't change either input query. (@germi Yeah, it's magic :) ) – Rawling Dec 20 '13 at 11:24
  • @Rawling That's ... nice. Didn't know that. – germi Dec 20 '13 at 11:27
  • 2
    @germi See here: http://stackoverflow.com/a/2299707/607861 - if you initialize anonymous objects with the same properties in 2 places in a program, the compiler makes sure the same type is used for both. This is very handy! – Neil Vass Dec 20 '13 at 11:29
  • 2
    @Germi Yeah, it's nice - anonymous types get useful equality behaviour and `Union` happily takes advantage of it. From the spec, *The `Equals` and `GetHashcode` methods on anonymous types override the methods inherited from `object`, and are defined in terms of the `Equals` and `GetHashcode` of the properties, so that two instances of the same anonymous type are equal if and only if all their properties are equal.* – Rawling Dec 20 '13 at 11:30
  • Thankyou guys Lamba/Linq is a fairly new concept to me as you can probably tell. – Daryl Leak Dec 20 '13 at 11:31

3 Answers3

3

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).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

I think you mean to say:

results = results.Concat(resultsSetup);
Sean
  • 60,939
  • 11
  • 97
  • 136
1

Try this...

results = results.Union(resultsSetup);
Davecz
  • 1,199
  • 3
  • 19
  • 43