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.

6
  • Does Union work on anonymous types? Commented Dec 20, 2013 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 :) ) Commented Dec 20, 2013 at 11:24
  • @Rawling That's ... nice. Didn't know that. Commented Dec 20, 2013 at 11:27
  • 2
    @germi See here: 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! Commented Dec 20, 2013 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. Commented Dec 20, 2013 at 11:30

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Oops can tell its nearly Christmas just couldn't see it myself. Thanks!
1

I think you mean to say:

results = results.Concat(resultsSetup);

Comments

1

Try this...

results = results.Union(resultsSetup);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.