LINQ querying a many-to-many relationship

Gary Chapman

I have an ASP.NET application that talks to an SQL database containing 4 tables:

  • Cities
  • States
  • StatesCities (maps Cities<=>States)

  • Customers (which stores the ID of the City they live in)

I need to get a count of how many customers live in a particular state. I can achieve this with the following SQL query:

select count(*) from Customers where CityID in 
(
    select sc.CityID from StatesCities sc, States s 
    where sc.StateID = s.StateID AND s.Name = 'Texas'
)

How can I express the equivalent of this query in LINQ, either using the EF or LINQ to SQL?

With the EF approach I've made it as far as:

var cities = db.Cities.Where(c => c.States.Any(s => s.Name == "Texas"));

but I'm not sure how to do the Customer/CityID match and count.

Gary Chapman

@216 got me on the right track with the first suggestion, but because a City can exist in more than one State I needed to tweak the statement slightly:

int customersCount = db.Customers.Count(c => c.City.States.Any(s => s.Name == "Texas"));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Querying a many to many relationship in SQL

From Dev

LINQ lambda for many to many relationship

From Dev

LINQ lambda for many to many relationship

From Dev

Sequelize Querying Many to Many Relationship and Accessing Object

From Dev

Querying many-to-many relationship in MySql

From Dev

Need a LINQ statement - many to many relationship

From Dev

Linq Query relating Many to Many relationship

From Dev

Need a LINQ statement - many to many relationship

From Dev

Querying a Collection of One-to-Many Relationship

From Dev

Querying a model by a many_many relationship with Silverstripe DataModel

From Dev

Excluding Item from Result List When Querying by Many to Many Relationship

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 Retrieve many to one relationship database

From Dev

How to retrieve fellow students from many-to-many relationship with Linq

From Dev

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

From Dev

How can I write a simple LINQ report for a many to many relationship?

From Dev

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

From Dev

How to convert SQL, many to many relationship, to Linq statement

From Dev

Query many-to-many relationship with linq c#

From Dev

Querying for parent object in Mongoid has_many relationship

From Dev

Querying related fields from Django Tables with One to Many Relationship

From Dev

Is this a many-to-many relationship?

From Dev

Many to Many relationship in Ecto

From Dev

Many to many relationship and MySQL

From Dev

implementing a many to many relationship

From Dev

Many to many relationship optimization

From Dev

many to many powerpivot relationship

From Dev

Sqlalchemy many to many relationship

Related Related

  1. 1

    Querying a many to many relationship in SQL

  2. 2

    LINQ lambda for many to many relationship

  3. 3

    LINQ lambda for many to many relationship

  4. 4

    Sequelize Querying Many to Many Relationship and Accessing Object

  5. 5

    Querying many-to-many relationship in MySql

  6. 6

    Need a LINQ statement - many to many relationship

  7. 7

    Linq Query relating Many to Many relationship

  8. 8

    Need a LINQ statement - many to many relationship

  9. 9

    Querying a Collection of One-to-Many Relationship

  10. 10

    Querying a model by a many_many relationship with Silverstripe DataModel

  11. 11

    Excluding Item from Result List When Querying by Many to Many Relationship

  12. 12

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

  13. 13

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

  14. 14

    LINQ Retrieve many to one relationship database

  15. 15

    How to retrieve fellow students from many-to-many relationship with Linq

  16. 16

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

  17. 17

    How can I write a simple LINQ report for a many to many relationship?

  18. 18

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

  19. 19

    How to convert SQL, many to many relationship, to Linq statement

  20. 20

    Query many-to-many relationship with linq c#

  21. 21

    Querying for parent object in Mongoid has_many relationship

  22. 22

    Querying related fields from Django Tables with One to Many Relationship

  23. 23

    Is this a many-to-many relationship?

  24. 24

    Many to Many relationship in Ecto

  25. 25

    Many to many relationship and MySQL

  26. 26

    implementing a many to many relationship

  27. 27

    Many to many relationship optimization

  28. 28

    many to many powerpivot relationship

  29. 29

    Sqlalchemy many to many relationship

HotTag

Archive