English 中文(简体)
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.

  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.





相关问题
Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Is List<> better than DataSet for UI Layer in ASP.Net?

I want to get data from my data access layer into my business layer, then prepare it for use in my UI. So i wonder: is it better to read my data by DataReader and use it to fill a List<BLClasses&...

What is the benefit to using List<T> over IEnumerable<T>?

or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at ...

灵活性:在滚动之前显示错误的清单

我有一份清单,在你滚动之前没有显示任何物品,然后这些物品就显示。 是否有任何人知道如何解决这一问题? 我尝试了叫人名单。

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签