Linq Query relating Many to Many relationship

Salman Shaykh

i am trying to write a LINQ query but somehow it is not working, here is the query

var Table3Qry =
   from LoginTable in db.Logins 
   from ServerTable in LoginTable.Servers.Where(x => x.ServerID == id) 
   select new { LoginTable.passwd, LoginTable.LoginID };

Table Structures are

Login

 - Passwd
 - Userid 
 - Loginid

Server

 - Serverid
 - Servername

ServerLogins

 - Serverid
 - Loginid

I want to find all the Passwords in Login table to a specific ServerID (named id in the query).

Sergey Berezovskiy

Filter servers by id, then select all passwords from each server logins:

var passwords =
     db.Servers.Where(s => s.ServerID == id)
       .SelectMany(s => s.Logins.Select(l => new { l.Passwd, l.LoginId }));

Query syntax:

var passwords = from s in db.Servers
                where s.ServerID == id
                from l in s.Logins
                select new { l.Passwd, l.LoginId };

If you are starting from Logins table:

var passwords = from l in db.Logins
                where l.Servers.Any(s => s.ServerID == id)
                select new { l.Passwd, l.LoginId };

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there a way to simplify a Linq query with a many to one relationship?

From Dev

Is there a way to simplify a Linq query with a many to one relationship?

From Dev

Linq Many to Many query

From Dev

Linq Many to Many query

From Dev

Query a many-to-many relationship with linq/Entity Framework. CodeFirst

From Dev

how to write a Linq query with a EF code first Many to Many relationship

From Dev

Query many-to-many relationship with linq c#

From Dev

LINQ lambda for many to many relationship

From Dev

LINQ lambda for many to many relationship

From Dev

Query many to many relationship with DetachedCriteria

From Dev

how to query a many to many relationship?

From Dev

eloquent: query many to many relationship

From Dev

Many to Many database query with LINQ

From Dev

query and create objects with a one to many relationship using LINQ

From Dev

Need a LINQ statement - many to many relationship

From Dev

LINQ querying a many-to-many relationship

From Dev

Need a LINQ statement - many to many relationship

From Dev

JPA criteria query in a many-to-many relationship

From Dev

Parse - Array of pointers - Many to Many relationship query

From Dev

CoreData: Query to one-to-many-to-many relationship

From Dev

Many-to-many relationship query alright?

From Dev

Advanced SQLite query on many-to-many relationship

From Dev

Laravel Many to Many query relationship with where clause

From Dev

Hibernate query on many to many relationship with extra column

From Dev

JPQL query with many-to-many relationship

From Dev

ios parse one query many to many relationship

From Dev

Many to Many relationship query. Laravel

From Dev

query over many to many relationship in redbeanphp

From Dev

Laravel Many to Many query relationship with where clause

Related Related

  1. 1

    Is there a way to simplify a Linq query with a many to one relationship?

  2. 2

    Is there a way to simplify a Linq query with a many to one relationship?

  3. 3

    Linq Many to Many query

  4. 4

    Linq Many to Many query

  5. 5

    Query a many-to-many relationship with linq/Entity Framework. CodeFirst

  6. 6

    how to write a Linq query with a EF code first Many to Many relationship

  7. 7

    Query many-to-many relationship with linq c#

  8. 8

    LINQ lambda for many to many relationship

  9. 9

    LINQ lambda for many to many relationship

  10. 10

    Query many to many relationship with DetachedCriteria

  11. 11

    how to query a many to many relationship?

  12. 12

    eloquent: query many to many relationship

  13. 13

    Many to Many database query with LINQ

  14. 14

    query and create objects with a one to many relationship using LINQ

  15. 15

    Need a LINQ statement - many to many relationship

  16. 16

    LINQ querying a many-to-many relationship

  17. 17

    Need a LINQ statement - many to many relationship

  18. 18

    JPA criteria query in a many-to-many relationship

  19. 19

    Parse - Array of pointers - Many to Many relationship query

  20. 20

    CoreData: Query to one-to-many-to-many relationship

  21. 21

    Many-to-many relationship query alright?

  22. 22

    Advanced SQLite query on many-to-many relationship

  23. 23

    Laravel Many to Many query relationship with where clause

  24. 24

    Hibernate query on many to many relationship with extra column

  25. 25

    JPQL query with many-to-many relationship

  26. 26

    ios parse one query many to many relationship

  27. 27

    Many to Many relationship query. Laravel

  28. 28

    query over many to many relationship in redbeanphp

  29. 29

    Laravel Many to Many query relationship with where clause

HotTag

Archive