Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加入linq_Linq_Entity Framework - Fatal编程技术网

加入linq

加入linq,linq,entity-framework,Linq,Entity Framework,是否可以在linq中进行连接,并且只从存在另一个键的一个数据集返回数据,有点像: var q = from c in customers join o in orders on c.Key equals o.Key select new {c.Name, o.OrderNumber}; 然后,不再只返回两条记录,而是返回客户,如: var q = from c in customers join o in o

是否可以在linq中进行连接,并且只从存在另一个键的一个数据集返回数据,有点像:

 var q = from c in customers
            join o in orders on c.Key equals o.Key
            select new {c.Name, o.OrderNumber};
然后,不再只返回两条记录,而是返回客户,如:

  var q = from c in customers
                join o in orders on c.Key equals o.Key
                select c;
当我尝试做(类似的事情)时,会出现以下错误:
指定的LINQ表达式包含对与不同上下文关联的查询的引用。

我假设您跳过了涉及orders表的Where子句(否则连接将毫无意义)

在这种情况下,您可以让Linq推断连接

var q = from c in customers
       where c.Orders.Any(o=> o.ProductCode == productCode)
       select c;

如果定义了外键,Linq2Sql将自动创建Orders属性;我相信对于实体框架,您必须手动指定它。

该错误表示另一个问题:
如果使用Linq to SQL,则必须对查询中的每个对象使用相同的DataContext

您的代码应该是这样的:

using (MyDataContext dc = new MyDataContext())
{
    var q = from c in dc.customers
            join o in dc.orders on c.Key equals o.Key
            select c;

    // process q within the DataContext
    // the easies would be to call q.ToList()
}

在EF4.0中,是否会从多个上下文创建联接

例如:

TestModelEntities e1 = new TestModelEntities();
        TestModelEntities e2 = new TestModelEntities();

        var d = from firme1 in e1.firme
                join g in e2.grad on firme1.grad.grad_id equals g.grad_id
                select firme1;

客户和订单都在同一个DataContext中吗?我同意Stephen的观点,您使用的是同一个DataContext吗?错误消息似乎非常具体。我敢说他没有跳过它。毕竟,第一段代码看起来是正确的。考虑到所描述的错误文本,我的假设是他正在尝试合并两个数据上下文。我假设存在一个WHERE,因为如果没有它,该行可能会被写为“var q=from c in customers select c;”