LINQ multi-table query: error on INCLUDE
Query: Given a cuisineID (type of cuisine), return the list of restaurants that offer that cuisine and also return the city for each og those restauarants (1 per restaurant). The RESTAURANT and CITY tables have a foreign-key relationship based on CITY_ID. I have added a Linq to SQL .dbml file (FVRG.dbml) and loaded all the tables for my database in the FVRGdatacontext file. When I look at the file, I can see the foreign key relationships between each of the tables. However, Intellisense does not recognize usage of the include statement and returns the following error. Can you please help? Regards, Susan
Error 1: 'System.Data.Linq.Table' does not contain a definition for 'Include' and no extension method 'Include' accepting a first argument of type 'System.Data.Linq.Table' could be found (are you missing a using directive or an assembly reference?) H:\ASP.Net\FVRG\DL\DLgetRestaurants.cs 20 68 DL
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace DL
{
public class DLgetRestaurants
{
DL.FVRGDataContext db = new FVRGDataContext();
开发者_JS百科 public IEnumerable <RESTAURANT> getRestaurants(string cuisineName)
{
var cuisineID = db.CUISINEs.First(s => s.CUISINE_NAME == cuisineName).CUISINE_ID;
var restaurantList = from RESTAURANT in db.RESTAURANTs.Include("CITies")
where RESTAURANT.CITY.Any(t => t.CITY_ID == 2)
select RESTAURANT;
return restaurantList;
}
}
}
var db = new YourDataContext();
List<Restaurant> result = (from c in db.Cities
join r in db.Restaurants on c.CityID equals r.CityID
where c.CuisineName == cuisineName && c.CityID == cityID
select r).ToList();
return result;
That is, of course, as long as you have a database table called Cities
and a table called Restaurants
with a CityID
foreign key to the primary key of the Cities
table.... Does that make sense?
EDIT
If you wanted multiple joins you could have something like this...
List<Restaurant> result = (from c in db.Cities
join r in db.Restaurants on c.CityID equals r.CityID
join x in db.SomethingElse on r.SomethingID equals x.SomethingID
where c.CuisineName == cuisineName && c.CityID == cityID
select r).ToList();
LINQ To SQL doesn't support the Include()
extension method, as in EF.
I suspect you're wanting .LoadWith()
instead on the DataLoadOptions.
var db = new MyDataContext();
var dlo = new DataLoadOptions();
dlo.LoadWith<Cities>(p => p.Restaurants);
db.LoadOptions = dlo;
var restaurantList = from RESTAURANT in db.RESTAURANTs
where RESTAURANT.CITY.Any(t => t.CITY_ID == cuisineID)
select RESTAURANT;
精彩评论