none
linq query to filter based on related table RRS feed

  • Question

  • My poco looks like

    Public class Obj1 {

        Public string Name {get; set;}

        Public ICollection<Obj2> Items {get; set;}

    }

    Public class Obj2 {

        Public string Name {get; set;}

    }

    So far what I have written is

    var x = from obj1 in Obj1 where obj1.Name = 'Foo' Select obj1;

    But I want filter on the name property in the list of Obj2 as well. How can I say that give me all items where obj1.Name is Foo and any item in the array Items of Obj2 where the Name is Bar.

    I am sure this is FAQ... so sorry if this is very basic.


    val it: unit=()


    Monday, May 12, 2014 8:59 PM

Answers

  • The following query should return all Obj1 items with a name equal to "Foo" and that contains at least one Obj2 object with a name equal to "Bar":

    var x = from obj1 in Obj1 
                        where obj1.Name.Equals("Foo")
                        && obj1.Items != null && obj1.Items.Any(i=>i.Name.Equals("Bar"))
                        select obj1;
    

    Monday, May 12, 2014 9:05 PM

All replies

  • The following query should return all Obj1 items with a name equal to "Foo" and that contains at least one Obj2 object with a name equal to "Bar":

    var x = from obj1 in Obj1 
                        where obj1.Name.Equals("Foo")
                        && obj1.Items != null && obj1.Items.Any(i=>i.Name.Equals("Bar"))
                        select obj1;
    

    Monday, May 12, 2014 9:05 PM
  • Hi MSDN Student;

    Does your classes Obj1 and Obj2 contain primary key fields that are not shown in the question?


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, May 13, 2014 2:52 AM
  • Oh yes. both objects have ID fields. I did not put them above for simplicity.

    val it: unit=()

    Tuesday, May 13, 2014 5:24 AM
  • Hello MSDN Student,

    Any update? I have marked Magnuss reply as answer, if you think it provides no help, please unmark it.

    Thank you for your understanding and support.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 22, 2014 8:25 AM
    Moderator