0

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

David A Gibson
  • 2,013
  • 5
  • 35
  • 60

4 Answers4

1

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 Mains
  • 50,520
  • 35
  • 148
  • 257
  • 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 Jun 01 '10 at 08:50
0

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
  • 1,137
  • 1
  • 10
  • 23
  • Hi Patrick, I've left work now but this looks good, I will try it tomorrow and let you know, thanks – David A Gibson Jan 28 '10 at 17:44
  • 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 Jan 29 '10 at 09:19
0

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
  • 2,013
  • 5
  • 35
  • 60
0

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
  • 1,137
  • 1
  • 10
  • 23
  • 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 Feb 03 '10 at 10:26