【问题标题】:Linq-to-entities orderby gives ambiguous column name errorLinq-to-entities orderby 给出不明确的列名错误
【发布时间】: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


    【解决方案1】:

    您是否尝试将orderby 移动到查询的末尾 select 关键字之后? 喜欢:

    actions.OrderBy(a => a.DateCreated);
    

    【讨论】:

    • 是的,我刚刚尝试过,它有效。我已经修改了OP。感谢您的帮助。
    【解决方案2】:

    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
    }
    

    【讨论】:

    • 最后一个属性实际上是后来添加的,不影响整体结果。 @Pr0fess0rX 给出了几乎正确的答案。谢谢。
    • @SteveA:是的,我的回答更像是一个赃物。我一看到他就给他投了赞成票。
    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    相关资源
    最近更新 更多