0

I have the following select query in linq:

var something = from te in taskEvidences join e in evidences on te.EvidenceId equals e.Id join tr in taskRequirements on te.TaskListId equals tr.TaskListId join r in newSelectableModule.Requirements on tr.RequirementListId equals r.Requirement.Id select new { Evidence = e, RequirementIndices = r.Index }; 

Currently it selects an Evidence object along with several Index (int) values, so for example I might get 5 records back, all with the same Evidence object and 5 different indices.

What I want to do it just return a single record with the Evidence object and a List<int> of the indices. I attempted to use grouping, but I keep getting errors about the type can't be inferred from the usage. This is one such attempt:

group new {e, r} by new {e} into g select new { Evidence = g.Key, RequirementIndices = g.SelectMany(x => x.r.Index) }; 

The error occurs around the SelectMany being assigned to the RequirementIndices property. I've tried several suggests I've found online, but none of them have helped. I assume it is a small error on my part, but I'm going code blind now!

Update:

Exact error:

The type arguments for method 'Enumerable.SelectMany(IEnumerable, Func>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

4
  • 4
    So what's the exact error? A minimal reproducible example would make it a lot easier to help you. Commented Mar 3, 2017 at 12:16
  • 1
    SelectMany is to flatten a list of lists. Joining tables doesn't nest lists into lists. What if you change it to Select? BTW, r seems to be part of the grouping. Commented Mar 3, 2017 at 12:17
  • @JonSkeet I've updated the question with the exact error. Commented Mar 3, 2017 at 12:58
  • @JeroenvanLangen You were right, I only needed a Select rather than SelectMany! Told you I was going code blind! Commented Mar 3, 2017 at 13:11

2 Answers 2

1

As @JeroenvanLangen suggested in the comment on my question, I didn't need SelectMany only a Select:

var something = from te in taskEvidences join e in evidences on te.EvidenceId equals e.Id join tr in taskRequirements on te.TaskListId equals tr.TaskListId join r in newSelectableModule.Requirements on tr.RequirementListId equals r.Requirement.Id group new { e, r } by new { e } into g select new { Evidence = g.Key, RequirementIndices = g.Select(x => x.r.Index).ToList() }; 
Sign up to request clarification or add additional context in comments.

Comments

0

You should be able to produce the same grouped result by avoiding a join in the top level:

var something = from te in taskEvidences join e in evidences on te.EvidenceId equals e.Id select new { Evidence = e, RequirementIndices = ( from tr in taskRequirements join r in newSelectableModule.Requirements on tr.RequirementListId equals r.Requirement.Id where te.TaskListId equals tr.TaskListId select r.Index ).ToList() }; 

Now the list is selected through a correlated subquery with a join, which eliminates creation of "duplicates" of the parent record. It should have the same performance as the original query.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.