LINQ query for finding Parent by Child

Rostislav Borodin

I'm new to EF\LINQ. How to rewrite such sql query in LINQ:

SELECT Pacient_ID FROM VisitDates WHERE ID in 
(SELECT visitDate_ID from    Reviews WHERE comments LIKE N'%name%')

Problem is that I can't access colums created by EF in LINQ query (visitDate_ID, Pacient_ID) I'v tried to do something like this, but it is very slow:

List<Pacient> found = new List<Pacient>();
            List<Pacient> pacients = db.Pacients.Include(p => p.visits.Select(w => w.reviews)).ToList();
            bool k = false;
            foreach (Pacient p in pacients)
            {
                foreach (VisitDate date in p.visits)
                {
                    foreach (Review r in date.reviews)
                    {
                        if (r.comments.ToLower().Contains(name.ToLower()))
                        {
                            found.Add(p);
                            k = true;
                            break;
                        }
                    }
                    if (k)
                    {
                        k = false;
                        break;
                    }
                }
            }

Thanks!

Robert McKee

This will do what your SQL query asked for:

var results=db.VisitDates
  .Where(vd=>vd.Reviews.Any(r=>r.comments.Contains("name")))
  .Select(vd=>vd.Pacient_ID);

.ToLower() stuff isn't required (usually) because the comparison is handled by the database, which by default does case insensitive comparisons, and adding them may cause the query to run significantly slower.

If you want pacients, then it'd be:

var results=db.Pacients
  .Where(p=>p.VisitDates.Any(vd=>vd.Reviews.Any(r=>r.comments.Contains("name"))));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

LINQ Query for outerjoining Parent and child (nested) tables

From Dev

Linq query to orderby level then parent and child item

From Dev

Linq to sql query with Parent child relationship

From Dev

Linq order by parent and child

From Dev

Finding parent records with children in LINQ

From Dev

Order Query by Parent and Child

From Dev

Watir: Finding the Parent div containing a specific child

From Dev

Finding parent from child in XML using python

From Dev

linq query - get parent entity where at least one child entity is part of list

From Dev

SQL parent child table query

From Dev

LINQ Sum Query on a Child element

From Dev

mysql query finding parent from children records

From Dev

Linq get child from Parent.Child.child

From Dev

Linq group by parent property order by child

From Dev

Recursive Linq - Parent indication for child property?

From Dev

Joining two datatables as parent-child in linq

From Dev

How to traverse parent child data using Linq

From Dev

Joining two datatables as parent-child in linq

From Dev

Recursive Linq - Parent indication for child property?

From Dev

Linq to XML Select All Parent and Child Items

From Dev

LINQ - get parent based on last child condition

From Dev

Finding child element by class from parent with pure javascript cross browser

From Dev

Finding child element by class from parent with pure javascript cross browser

From Dev

LINQ lambda expression, finding a child object where property is == X

From Dev

Bool query over child and parent fields

From Dev

json query that returns parent element and child data?

From Dev

Query by a parent field but return a child class

From Dev

Query effeciency, 3 child tables to 1 parent

From Dev

Recursive query challenge - simple parent/child example

Related Related

  1. 1

    LINQ Query for outerjoining Parent and child (nested) tables

  2. 2

    Linq query to orderby level then parent and child item

  3. 3

    Linq to sql query with Parent child relationship

  4. 4

    Linq order by parent and child

  5. 5

    Finding parent records with children in LINQ

  6. 6

    Order Query by Parent and Child

  7. 7

    Watir: Finding the Parent div containing a specific child

  8. 8

    Finding parent from child in XML using python

  9. 9

    linq query - get parent entity where at least one child entity is part of list

  10. 10

    SQL parent child table query

  11. 11

    LINQ Sum Query on a Child element

  12. 12

    mysql query finding parent from children records

  13. 13

    Linq get child from Parent.Child.child

  14. 14

    Linq group by parent property order by child

  15. 15

    Recursive Linq - Parent indication for child property?

  16. 16

    Joining two datatables as parent-child in linq

  17. 17

    How to traverse parent child data using Linq

  18. 18

    Joining two datatables as parent-child in linq

  19. 19

    Recursive Linq - Parent indication for child property?

  20. 20

    Linq to XML Select All Parent and Child Items

  21. 21

    LINQ - get parent based on last child condition

  22. 22

    Finding child element by class from parent with pure javascript cross browser

  23. 23

    Finding child element by class from parent with pure javascript cross browser

  24. 24

    LINQ lambda expression, finding a child object where property is == X

  25. 25

    Bool query over child and parent fields

  26. 26

    json query that returns parent element and child data?

  27. 27

    Query by a parent field but return a child class

  28. 28

    Query effeciency, 3 child tables to 1 parent

  29. 29

    Recursive query challenge - simple parent/child example

HotTag

Archive