0
public class ConvertedReading
{
    public int DatePointID { get; set; }
    public string DatapointName { get; set; }
    public DateTime ReadingDate { get; set; }
    public double Value { get; set; }
}

Given a collection below:

    IEnumerable<ConvertedReading> readingData;

using c# , write a LINQ statment using lambda syntax to output a collection of the following:

public class DataPointTotal
{
    public string DatapointName{get; set;}
    public double Total{get; set;}
}

one item should be returned per datepointID

Total should reprenst the sum of convertedReading.valye relevent to each datePointID

I have tried with the following, nit quite shre if it works, i cant seems to get the sum of value right.

var subtotals = from x in ConvertedReading group x by x.DatapointName into g select new { Type = x.DatapointName , SubTotal = g.Sum(x => x.Total) };

Is the syntax correct?

Thanks guys,

2
  • var subtotals = from x in ConvertedReading group x by x.DatapointName into g select new { Type = x.DatapointName , SubTotal = g.Sum(x => x.Total) }; Commented Mar 21, 2013 at 21:44
  • 2
    @Alborz: Edit that into the question, and explain what's wrong with it... Commented Mar 21, 2013 at 21:45

2 Answers 2

2

The answer is:

var result = readingData.GroupBy(r => r.DataPointID)
                        .Select(g => new DataPointTotal
                                {
                                    DataPointName = g.First().DataPointName,
                                    Total = g.Sum(r => r.Value)
                                });

But I guess that's cheating considering I wrote the question! ;)

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

3 Comments

Why did you ask for the answer in "lambda syntax" (actually called method syntax)? It doesn't seem fair to restrict a candidate as such...
I didn't know it was called method syntax!! The questions do not all have to be completed, they are to gauge people's current knowledge base. Also, we only use "method" syntax at work as it reads better (subjective). Good on @Alborz anyway, when I don't know how to do something I also turn to the internet!
Yep, definately called Method Syntax. See here. There's a cool product called Technically Compatible for recruitment tests, analysis, benchmarking, and reporting. It has tens of thousands of questions like this. Plug over! :)
0
var result = db.ConvertedReading.GroupBy(i=>i.DatapointName).Select(g => new DataPointTotal(){DatapointName = g.DatapointName, Total = g.Sum(i=>i.Total)});

2 Comments

Wrong. You can't assume that DatapointName and DataPointID are related one-to-one.
@flem, the code is based on the info in the question, I simply converted his code into lambda expressions w/o assuming anything :)

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.