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

John S

I am using Linq to entities to query a contact record that has 0 to 4 street addresses. The address that I am trying to get is the address with an address type of "Primary". This query gets the information that I want and returns it to an array which is passed out of a JSON method to my front end.

 var data = db.Contacts
            .Where(a => a.LastName.Contains(term))
            .Select(r => new
            {
                r.Id,
                value = r.LastName + ", " + r.FirstName + " " + r.MiddleName,
                r.Email,
                r.Title,
                Street1 = c.Addresses
                    .Where(a => a.AddressType.Name.Contains("Primary"))
                    .FirstOrDefault().Street1
                ,
                Street2 = c.Addresses
                    .Where(a => a.AddressType.Name.Contains("Primary"))
                    .FirstOrDefault().Street2
                ,
                City = c.Addresses
                    .Where(a => a.AddressType.Name.Contains("Primary"))
                    .FirstOrDefault().City
                ,
                State = c.Addresses
                    .Where(a => a.AddressType.Name.Contains("Primary"))
                    .FirstOrDefault().State
                ,
                Postal = c.Addresses
                    .Where(a => a.AddressType.Name.Contains("Primary"))
                    .FirstOrDefault().PostalCode
                ,
                r.Institution.Name
            })
            .Take(10).ToArray();

What I am wondering is if there is a way to simplify the portion of the query ** .Where(a => a.AddressType.Name.Contains("Primary"))** so that I don't have the same section repeated each time?

Gert Arnold

You can use the let keyword:

from a in db.Contacts
where a.LastName.Contains(term)
let primaryAddress = a.Addresses
                    .Where(a => a.AddressType.Name.Contains("Primary"))
                    .FirstOrDefault()
select new 
{
    a.Id,
    value = a.LastName + ", " + a.FirstName + " " + a.MiddleName,
    a.Email,
    a.Title,
    Street1 = primaryAddress.Street1,
    Street2 = primaryAddress.Street2,
    City = primaryAddress.City,
    State = primaryAddress.State,
    Postal = primaryAddress.PostalCode,
    a.Institution.Name
})

This is one of those case where I prefer comprehensive syntax to fluent syntax, because it produces much better readable code. Under the hood it is converted to fluent syntax with two Selects:

db.Contacts
    .Where(a => a.LastName.Contains(term))
    .Select(a => 
    new 
    { 
        a = a,
        primaryAddress = 
            a.Addresses
                .FirstOrDefault(a => a.AddressType.Name.Contains("Primary"))
    })
    .Select(a =>
    new 
    {
        a.a.Id,
        value = a.a.LastName + ", " + a.a.FirstName + " " + a.a.MiddleName,
        a.a.Email,
        a.a.Title,
        Street1 = a.primaryAddress.Street1,
        Street2 = a.primaryAddress.Street2,
        City = a.primaryAddress.City,
        State = a.primaryAddress.State,
        Postal = a.primaryAddress.PostalCode,
        a.a.Institution.Name
    })

Take your pick!

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

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

From Dev

Linq Query relating Many to Many relationship

From Dev

Two way one to many relationship

From Dev

Query One to Many Relationship SQLAlchemy

From Dev

JPA one to many relationship query

From Dev

Django query in One to Many relationship

From Dev

Django one to many Relationship Query

From Dev

Query One to Many Relationship SQLAlchemy

From Dev

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

From Dev

ios parse one query many to many relationship

From Dev

Laravel - One to Many relationship is not working one way

From Dev

Laravel - One to Many relationship is not working one way

From Dev

LINQ Retrieve many to one relationship database

From Dev

Simplify many LINQ Join

From Dev

Simplify many LINQ Join

From Dev

How to query a one to many/many to many relationship in Flask SQL Alchemy?

From Dev

Criteria query for unidirectional one-to-many relationship

From Dev

jooq single query with one to many relationship

From Dev

Data base One To Many Relationship Query

From Dev

Data base One To Many Relationship Query

From Dev

How to query one-to-many relationship in JPQL?

From Dev

query regarding one to many relationship in hibernate

From Dev

Criteria query for unidirectional one-to-many relationship

From Dev

Query two tables with one to many relationship

From Dev

Ruby on Rails Query a one to many relationship

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#

Related Related

  1. 1

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

  2. 2

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

  3. 3

    Linq Query relating Many to Many relationship

  4. 4

    Two way one to many relationship

  5. 5

    Query One to Many Relationship SQLAlchemy

  6. 6

    JPA one to many relationship query

  7. 7

    Django query in One to Many relationship

  8. 8

    Django one to many Relationship Query

  9. 9

    Query One to Many Relationship SQLAlchemy

  10. 10

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

  11. 11

    ios parse one query many to many relationship

  12. 12

    Laravel - One to Many relationship is not working one way

  13. 13

    Laravel - One to Many relationship is not working one way

  14. 14

    LINQ Retrieve many to one relationship database

  15. 15

    Simplify many LINQ Join

  16. 16

    Simplify many LINQ Join

  17. 17

    How to query a one to many/many to many relationship in Flask SQL Alchemy?

  18. 18

    Criteria query for unidirectional one-to-many relationship

  19. 19

    jooq single query with one to many relationship

  20. 20

    Data base One To Many Relationship Query

  21. 21

    Data base One To Many Relationship Query

  22. 22

    How to query one-to-many relationship in JPQL?

  23. 23

    query regarding one to many relationship in hibernate

  24. 24

    Criteria query for unidirectional one-to-many relationship

  25. 25

    Query two tables with one to many relationship

  26. 26

    Ruby on Rails Query a one to many relationship

  27. 27

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

  28. 28

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

  29. 29

    Query many-to-many relationship with linq c#

HotTag

Archive