我在Linq to entities(4.0)中创建了一个查询,这让我感到困惑,因为我连接了几个表,其中两个表共享一个列名(DateCreated)。当我试图在具有相同列名的两个表之间使用orderby时,出现以下错误:“column'DateCreated'in order子句不明确”
我很困惑,因为我认为指定表意味着它将传递给SQL查询。在下面的示例中,我指定了“ORDER BY a.Datecreated”,但在TSQL中,它只有orderbyDateCreated
,而我本希望看到orderbyExtent1
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。
编辑:
就在我问完这个问题之后,我看到了另一个基于左连接的问题,这导致我写下:
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个小时在这几天和几秒钟的张贴,我找到了答案!
最佳答案
您是否尝试将orderby
移动到select关键字之后的查询末尾?
比如:
actions.OrderBy(a => a.DateCreated);
关于c# - Linq-to-entities orderby给出了不明确的列名错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5717326/