views:

167

answers:

4

An Activity occurs at a location on a specific date, but each activity may be repeated at the same or different locations on different dates. I have created an entity framework model, and wish to populate it with relevant Activities which occur between two dates, ordered by the locations distance from a specified location.

I therefore have a the following tables:

Activity (A)

Occurrence (O)

Location (L)

With relationships as follows:

A 1-n O n-1 L

I am having some difficulty in doing this using linq to entities, but am sure that I can achieve it in a stored procedure.

Is this something that I should be able to do using Linq or is it too complex a problem for Linq to create the Sql? Any pointers about writing the Linq query would be appreciated, if it is something that it should be able to do.

+2  A: 

I don't see why that would be impossible to do, or even particularly difficult. Was that the question? :)

The activity table is not interesting to your query as I understand it, start by finding Occurrences. You can include the Activity data in the output like this:

dc.Occurrence.Include("Activity").Where(
    o => o.Date >= startDate && o.Date <= endDate
    && o.Location.DistanceFrom(someLocation) < maxDistance)

DistanceFrom would be however you determine the distance from a location.. I don't how anything about your database design.

If you're using SQL Server 2008 geocoding, I don't think that is supported yet. This article (and this continuation) might be of interest. It's about LINQ to SQL, but the expression building might be of help.

Thorarin
I resent the downvote :P The question was edited. Originally, it said nothing about giving pointers about the LINQ query...
Thorarin
Thanks, I don't see why the down vote, leaving it without comment is pretty unhelpful imho.. Thanks for the added links, I have seen the first, but the second really seems to answer my question. Because of the distance calc using linq is slow and therefore an sp is recommended. As an aside, your code does actually give a compile error: "Delegate 'System.Func<Occurrence,int,bool>' does not take 1' arguments". Any idea why that might be?
Richbits
Thoarin cool :) I just turned it into an upvote, I missed the edit sorry about that my bad.
olle
hmm I still don't see anything in the UI that indicates the question was edited, you ain't trying to trick me are you ;)
olle
@olle: I wasn't too fussed by the downvote, merely explaining my tongue-in-cheek answer. From my experience, if you edit very quickly after posting, it doesn't show as an edit. For example, I edited this post at least 6 times, fixing typos, etc. Four edits are showing however. Strange that you could even change it to an upvote however, it locked a downvote in after like 5 minutes earlier this week.
Thorarin
Richbits
A: 

In Linq2Sql, the designer would automatically create relationships in the L2S classes (but would not display them in the designer)

In Linq2Entities, the designer will display foreign key relationships, but will not automatically create them -- that has to be done manually. (that statement was based on a beta of Linq2Entities --- it may no longer be true).

James Curran
Relationships are automatically created.
Thorarin
no it gets them from the database if they are there. If you have a database with no relationships then you dont have relationships on your linq-2-sql data model untill you add them manually.
John Nicholas
+1  A: 

I have not used Linq, and maybe I am completely wrong but cant you use stored procedure even if you use Linq? ONE of the main reasons to use Stored procedure is to hide the underlaying datamodel from the business layer. By hiding the datamodel you can optimize the data access. The procedures is used as an interface to application and reports, and if you need to modify the data model this can be done without breaking the interface.

This is my opinion from a DBA perspective. As I said, I have no idea of what the puprose of Linq is.

Hakan Winther
yes, you are completley wrong - you can. In some cases the data processing should be done on a db as its simply far more efficient even if you are using a linq-2-sql model. I imagine some peopel would use this as an argument for not using linq-2-sql. I disagree, but would take a while to explain.
John Nicholas
I think we missunderstand each other. I am trying to say that if you use Linq with stored procedures it is okay. As a DBA I want the data processing to be done in the DB.
Hakan Winther
A: 
XXXXX
It can be done with LINQ, but I agree with Hakan Winther: a stored procedure seems like the more appropriate place to put this kind of logic.
XXXXX