Using Linq to select a list of Entities, linked Entities, linked Entities
Apologies for the poor question title - I'm not sure how to describe what I'm doing but that is the best I could come up with, please edit it if what I'm asking for has a real name!
I have Programmes, which can have a group of Projects assigned, which in turn have groups of Outputs assigned.
I would like to get all the outputs for the Programme through it's projects as one big list of outputs. I have this:
From pp In Me.ProgrammeProjects Select pp.Project.Outputs
Which basically gets me a list of output lists. (An Ienumerable Of EntitySet Of Output).
I'm brute forcing my way through Linq and can't find any examples of this (or can't recognise one when I see it). How can I do this using just Linq rather than for loops and Linq where I'开发者_如何学运维d go through each EntitySet and add it's contents to a bigger list?
Thanks
Or go against the linq context directly:
from o in context.Outputs
where o.Project.ProgrammeProjects.ID = 1
select o
The reverse will work too and query straight from the data context's table.
Are you trying to get a list of outputs for a specific programme?
If so, try something like this:
var result = (from pp in ProgrammeProjects
where pp.Name.Equals("ProjectA")
select pp.Project.Outputs).ToList();
or
once you get your list of outputs, you could use a lambda expression to get a subset.
var result = (from pp in ProgrammeProjects
select pp.Project.Outputs).ToList();
var subResult = result.FindAll(target => target.OutputParameter.Equals("findThisValue");
Is this what you're trying to do?
If not, give a bit more detail of the data structure and what you're trying to retrieve and I'll do my best to help.
Patrick.
This is the way I've resorted to doing it, but I can tell it's going to be slow when the amount of data increases.
Dim allOutputs As New Generic.List(Of Output)
Dim outputLists = From pp In Me.ProgrammeProjects Select pp.Project.Outputs.ToList
For Each outputList In outputLists
Dim os = From o In outputList Where o.OutputTypeID = Type Select o
allOutputs.AddRange(os)
Next
Return allOutputs
I'm still a bit confused as to what kind of data you're trying to retrieve. Here is what I understand.
- You have a list of Programmes
- Each Programme can have many Projects
- Each Project can have many outputs.
Goal: To find all outputs of a certain type. Is this right?
It doesn't look like you're retrieving any data related to the project or programme so something like this should work:
Dim allOutputs As Generic.List(Of Outputs) = (From output In Me.Outputs Where output.OutputType.Equals(Type) Select output).ToList()
Let me know how it goes.
Patrick.
精彩评论