Linq-to-entities orderby给出了不明确的列名错误

|| 我已经在Linq-to-entities(4.0)中创建了一个查询,当我加入几个表时,这使我感到困惑,其中两个表共享一个列名(DateCreated)。当我尝试在具有相同列名的两个表之间使用orderby时,出现以下错误:\'order \'DateCreated \'in order子句不明确\' 我很困惑,因为我认为指定表意味着将其传递给SQL查询。在下面的示例中,我指定\'orderby a.Datecreated \',但是在TSQL中,它只具有ORDER BY
DateCreated
,这时我希望看到ORDER BYѭ1DER.
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);
我在新的选择上添加了Orderby。现在可以使用了。但是,我仍然对为什么在主查询中使用orderby时不会做同样的事情感到困惑。嘿!几天后几天内,我花了大约5个小时,在发布后的几秒钟内,我发现了答案!     
已邀请:
您是否尝试过在select关键字之后将ѭ6移到查询的末尾? 喜欢:
actions.OrderBy(a => a.DateCreated);
    
TSQL可能作为特定的select new语句的部分构造而生成。您是否尝试过将属性名称更改为其他类似的名称:
select new
{
    ID = a.ID,
    FileID = a.FileID,
    WebPageID = a.WebPageID,
    UserID = a.UserID,
    FilePath = l1.Path,
    URLPath = l2.url,
    Created = a.DateCreated  // Change is here
}
    

要回复问题请先登录注册