Creating multiple results from a Linq query

I have a rather unique situation, I have never had to do this anyway. I have a Linq query that returns data from a database using EF4.1. I want to create several similar (identical signatures) anonymous (or even named if necessary) results of each query result.

Here is the code I'm using now:

var data = getMyData().Select(x => new { GoalName = x.GoalType.Name, Start = x.StartDate, End = x.EndDate, x.StartValue, x.CheckIns }).ToList(); var r1 = data.Select(x => new { title = x.GoalName, start = x.Start.ToString(), end = x.End.ToString(), className = "hidden", type = "goal" }); var r2 = data.Select(x => new { title = string.Format("Start: {0:0.##}", x.StartValue), start = x.Start.ToString(), end = x.Start.ToString(), className = "", type = "" }); var r3 = data.Select(x => new { title = "End", start = x.End.ToString(), end = x.End.ToString(), className = "", type = "" }); var r4 = data.SelectMany(x => x.CheckIns) .Select(y => new { title = y.CheckInValue.Value.ToString(), start = y.CheckInDateTime.ToString(), end = y.CheckInDateTime.ToString(), className = "", type = "" }); var result = r1.Union(r2).Union(r3).Union(r4); 

Now, perhaps this is just as good as everyone, but I cannot help but feel that something is missing.

Is there a better solution?

+4
source share
3 answers

What you have is actually OK, I think.

But the StevenzNPaul proposal is not so bad, here is how you can use the let keyword to store different forecasts, and then select the results individually (for brevity, I did not design all the fields, but you got the point):

 var query = from x in data let result1 = new {title = x.GoalName, start = x.Start} let result2 = new {title = string.Format("Start: {0:0.##}", x.StartValue), start = x.Start} let result3 = new {title = "End", start = x.End} let checkins = x.CheckIns.Select(checkin => new { title = "...", start = checkin.Start }) from result in new[] { result1, result2, result3 }.Concat(checkins) select result; 

Obviously, the best is a matter of preference. In addition, this will lead to a different order, which may or may not be a problem for you.

+3
source

You can create an iterator using yield , which also has the advantage of being evaluated lazily (does not require ToList ()). I created a typed Result class to store query results

 private IEnumerable<Result> PerformQuery() { var results= getMyData().Select(x => new {GoalName = x.GoalType.Name, Start = x.StartDate, End = x.EndDate, x.StartValue, x.CheckIns}); foreach (var result in results) { yield return new Result() { Title = result.GoalName, Start = result.Start.ToString(), End = result.End.ToString(), ClassName = "Hidden", Type = "Goal" }; yield return new Result() { Title = String.Format("Start: {0:0.##}",result.StartValue), Start = result.Start.ToString(), End = result.Start.ToString() } yield return new Result() { Title = "End", Start = result.End.ToString(), End = result.End.ToString() }; foreach (var checkIn in result.CheckIns) yield return new Result() { Title = checkIn.CheckInValue.Value.ToString(), Start = checkIn.CheckInDateTime.ToString(), End = checkIn.CheckInDateTime.ToString() }; } } 
+3
source

try using the let keyword, which will work for you.

+1
source

All Articles