views:

251

answers:

4

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

A: 

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.

Patrick
Hi Patrick, I've left work now but this looks good, I will try it tomorrow and let you know, thanks
David A Gibson
Hi Patrick, I tried your code and couldn't get what I wanted. Instead I've posted my solution - which isn't very good but should give you an idea of the final result I'm after. Cheers
David A Gibson
+1  A: 

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.

Brian
I think this is the best option - I didn't think of going against Outputs directly - this is by far the easiest option - I just need to create a repository for Outputs... cheers Brian
David A Gibson
A: 

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
David A Gibson
A: 

Hey David,

I'm still a bit confused as to what kind of data you're trying to retrieve. Here is what I understand.

  1. You have a list of Programmes
  2. Each Programme can have many Projects
  3. 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.

Patrick
Hi Patrick, your right in that I want a list of outputs of a type but I don't have a list of outputs to select from. I have a list of lists. I want to merge all the lists into a single one. The for loopin my answer is how I've done it but I thought Linq would have a better way. Cheers
David A Gibson