【发布时间】:2011-04-19 13:43:54
【问题描述】:
我在 Linq-to-entities (4.0) 中创建了一个查询,这让我很困惑,因为我加入了几个表,其中两个共享一个列名 (DateCreated)。当我尝试在具有相同列名的两个表之间使用 orderby 时,出现以下错误:'order 子句中的列 'DateCreated' 不明确'
我很困惑,因为我认为指定表意味着它将传递给 SQL 查询。在下面的示例中,我指定了“orderby a.Datecreated”,但在 TSQL 中它只有 ORDER BY DateCreated,而我本来希望看到 ORDER BY Extent1.DateCreated。
using (PDFActionsEntities pdfEntities = new PDFActionsEntities())
{
var actions = (from a in pdfEntities.PDFActions
join f in pdfEntities.Files on a.FileID equals f.FileID into list1
from l1 in list1.DefaultIfEmpty()
join wp in pdfEntities.WebPages on a.WebPageID equals wp.webpageid into list2
from l2 in list2.DefaultIfEmpty()
orderby a.DateCreated
select new
{
ID = a.ID,
FileID = a.FileID,
WebPageID = a.WebPageID,
UserID = a.UserID,
FilePath = l1.Path,
URLPath = l2.url,
DateCreated = a.DateCreated
});
}
这是它创建的 T-SQL
SELECT
`Extent1`.`FileID`,
`Extent1`.`ID`,
`Extent1`.`WebPageID`,
`Extent1`.`UserID`,
`Extent2`.`Path`,
`Extent3`.`url`,
`Extent1`.`DateCreated`
FROM `tblpdfactions` AS `Extent1` LEFT OUTER JOIN
`tblfiles` AS `Extent2` ON `Extent1`.`FileID` = `Extent2`.`FileID` LEFT OUTER JOIN
`tblwebpageprints` AS `Extent3` ON `Extent1`.`WebPageID` = `Extent3`.`webpageid`
ORDER BY `DateCreated` ASC
我是否遗漏了什么或做错了什么?
附:如果这有什么不同,它会连接到 MySQL。
编辑:
刚问完这个问题,我看到另一个基于Left Join的问题,于是我写了:
var actions = (from a in pdfEntities.PDFActions
join f in pdfEntities.Files on a.FileID equals f.FileID into list1
from l1 in list1.DefaultIfEmpty()
join wp in pdfEntities.WebPages on a.WebPageID equals wp.webpageid into list2
from l2 in list2.DefaultIfEmpty()
select new
{
ID = a.ID,
FileID = a.FileID,
WebPageID = a.WebPageID,
UserID = a.UserID,
FilePath = l1.Path,
URLPath = l2.url,
DateCreated = a.DateCreated
}).OrderBy(x => x.DateCreated);
我在 select new 上添加了 Orderby。这现在有效。但是,我仍然对为什么当 orderby 在主查询中时它不会做同样的事情感到困惑。嘿嗬!有点烦人,我花了几天时间在这上面花了大约 5 个小时,在发布后的几秒钟内,我找到了答案!
【问题讨论】:
标签: c# mysql entity-framework linq-to-entities