C# Linq to entities orderby提供了不明确的列名错误
我在LINQtoEntities(4.0)中创建了一个查询,这让我感到困惑,因为我连接了几个表,其中两个表共享一个列名(DateCreated)。当我尝试在具有相同列名的两个表之间使用orderby时,我得到了以下错误:“order子句中的列'DateCreated'不明确” 我感到困惑,因为我认为指定表意味着它将传递给SQL查询。在下面的示例中,我指定了“orderby a.Datecreated”,但在TSQL中,它只有ORDER BYC# Linq to entities orderby提供了不明确的列名错误,c#,mysql,entity-framework,linq-to-entities,C#,Mysql,Entity Framework,Linq To Entities,我在LINQtoEntities(4.0)中创建了一个查询,这让我感到困惑,因为我连接了几个表,其中两个表共享一个列名(DateCreated)。当我尝试在具有相同列名的两个表之间使用orderby时,我得到了以下错误:“order子句中的列'DateCreated'不明确” 我感到困惑,因为我认为指定表意味着它将传递给SQL查询。在下面的示例中,我指定了“orderby a.Datecreated”,但在TSQL中,它只有ORDER BYDatecreated,而我希望看到ORDER BYEx
Datecreated
,而我希望看到ORDER BYExtent1
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);
我在selectnew上添加了Orderby。这现在起作用了。然而,我仍然不明白为什么当orderby在主查询中时它不会做同样的事情。嘿嘿!有点恼人,我花了大约5个小时在这几天和几秒钟内张贴,我找到了答案 您是否尝试将
orderby
移动到查询末尾的select关键字后面?
比如:
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:是啊,我的回答更像是一个骗局。我一看到他,就投了他的赞成票。
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
}