LINQ多表查询:INCLUDE错误

时间:2011-07-11 13:49:29

标签: asp.net linq include

查询:鉴于烹饪ID(烹饪类型),返回提供该菜肴的餐厅列表,并为每个餐厅返回城市(每个餐厅1个)。 RESTAURANT和CITY表具有基于CITY_ID的外键关系。我已将Linq添加到SQL .dbml文件(FVRG.dbml)并在FVRGdatacontext文件中加载了我的数据库的所有表。当我查看文件时,我可以看到每个表之间的外键关系。但是,Intellisense无法识别include语句的用法并返回以下错误。你能帮忙吗?此致,苏珊

  

错误1:'System.Data.Linq.Table'不包含   'Include'的定义,没有扩展方法'Include'接受a   'System.Data.Linq.Table'类型的第一个参数可以   找到(你错过了使用指令或程序集   参考?)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();

        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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

LINQ To SQL不支持Include()扩展方法,如EF。

我怀疑你想要.LoadWith()代替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;

答案 1 :(得分:0)

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;

当然,只要你有一个名为Cities的数据库表和一个名为Restaurants的表,其CityID外键是{{1}的主键。表...这有意义吗?

修改

如果你想要多个连接,你可以拥有这样的东西......

Cities