2

I am making the journey from an SQL centric world (where I am reasonably fluent) to EF dDbContect with code first and I am struggling. The following is a simplified example; I want to write the following simple SQL query (which took 60 seconds to write):

SELECT
    HP.HospitalID
FROM
    Hospitals AS HP
    JOIN NHSTrusts AS NT
    ON NT.NHSTrustID = HP.NHSTrust_NHSTrustID

WHERE
    HP.HospitalName + ', ' + NT.NHSTrustName = 'My hospital name including trust'

as an EF style query. I cannot see how to do this and I don't want to drop back into SQL everytime I cannot see how to do something.

Can anyone help:

  • On how to frame the above query in EF dbContent
  • On a general source of help with
4
  • How Hospitals and NHSTrusts tables are related? Commented Oct 4, 2013 at 9:06
  • please note that WHERE HP.HospitalName='hospital name' AND NT.NHSTrustName = 'trust' could potentially be more efficient. Your approach excludes the usage of indexes Commented Oct 4, 2013 at 9:07
  • Each trust as a collection of hospitals. A hospital can only belong to one trust. The foreign key is HP.NHSTrust_NHSTrustID Commented Oct 4, 2013 at 9:10
  • Hi W0lf and thanks . The SQL query was just knocked up quickly as an example; translation is the problem, not efficiency. Commented Oct 4, 2013 at 9:12

4 Answers 4

3

Assuming your entities and DB Context are properly set, here's how your query may look like:

var hospitalIds = 
    from hp in dbContext.Hospitals
    where 
        hp.HospitalName == "..." &&
        hp.Trust.NHSTrustName == "..."
    select hp.HospitalId;

Of course, this query will have to be materialized by iterating through the results (or by using .ToList()/.ToArray()).

To define the entities and context properly, refer to this excellent tutorial on MSDN: http://msdn.microsoft.com/en-us/data/jj193542.

Sign up to request clarification or add additional context in comments.

1 Comment

I totally support this! If your relationships are set up properly you can make use of those relationship properties to create a very readable query.
2

Assuming these entity types:

public class Trust
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Hospital> Hospitals { get; set; }
}

public class Hospital
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int TrustId { get; set; }
    public Trust Trust { get; set; }
}

you'll have this query:

dbContext
    .Hospitals
    .Where(h => h.Name + h.Trust.Name == "My hospital name including trust")
    .Select(h => h.Id);

2 Comments

Hi Dennis, many thanks for this. A supplementary question; can any LINQ query also be written using lamda expressions.
@PeterSmith: these two ways are called "query syntax" and "method syntax" (my sample). You can start here: msdn.microsoft.com/en-us/library/vstudio/bb397947.aspx. In most cases, the choice between them is a question of personal preferences.
1

First try some Linq. You can write it as below. The query might have some mistake, it will be corrected by Intellisense.

var s  = (from c in Context.Hospitals
          from h in Context.NHSTrusts
          where c.NHSTrust_NHSTrustID==h.NHSTrustID 
          && string.Format("{0},{1}",c.HospitalName,h.NHSTrustName).Equals("My hospital name including trust")

          select c.HospitalId).FirstOrDefault();

3 Comments

You would've received my vote if you had used a join statement rather than 2 froms :)
Hi Amit, that's great. I was using lamda expressions. I really need to read up on LINQ.
What is the need for two from statements (or even join), if OP's entities should be related with the foreign key association?
0

For a good place to get some examples i would recommend 101 linq samples
It has everything from using a WHERE to GROUP BY statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.