【发布时间】:2010-02-04 23:47:33
【问题描述】:
我们在 dbml 文件中有以下测试模型:
Model http://www.freeimagehosting.net/uploads/a86582498a.gif
对于测试用例,表中有 4 条记录,1 条父项,3 条子项。我们正在寻找特定记录的兄弟姐妹,包括特定记录。
using (var db = new TestDataContext())
{
var query =
from f in db.Foos
where f.Name == "Two"
select f.Foo1.Foos; // get the record's parent's children
var foos = query.SelectMany(f => f); // project the EntitySet
Assert.AreEqual(3, foos.Count()); // passes
}
这将使用以下 SQL 返回正确的项目:
SELECT [t2].[FooId],
[t2].[ParentFooId],
[t2].[Name]
FROM [dbo].[Foos] AS [t0]
INNER JOIN [dbo].[Foos] AS [t1] ON [t1].[FooId] = [t0].[ParentFooId]
CROSS JOIN [dbo].[Foos] AS [t2]
WHERE ([t0].[Name] = @p0)
AND ([t2].[ParentFooId] = [t1].[FooId])
我们想知道 CROSS JOIN,这显然是 SelectMany 的结果?
为了避免 CROSS JOIN,我们还有其他方法可以解决这个问题吗?
【问题讨论】:
标签: c# sql linq-to-sql orm