C# LINQ非排序列表<&燃气轮机;适当地
我的EF查询应该按照列表中第一个产品的日期进行排序,但由于某些原因,它只对大多数产品进行排序,并且一些日期的顺序错误 这是密码C# LINQ非排序列表<&燃气轮机;适当地,c#,entity-framework,linq,predicatebuilder,linqkit,C#,Entity Framework,Linq,Predicatebuilder,Linqkit,我的EF查询应该按照列表中第一个产品的日期进行排序,但由于某些原因,它只对大多数产品进行排序,并且一些日期的顺序错误 这是密码 using (var context = new SalesEntities()) { var groupedData = context.s84_Schedule.AsExpandable() .Where(predicate) .GroupBy(c => new { c.CustomerID, c.s84_Customer.Cus
using (var context = new SalesEntities())
{
var groupedData = context.s84_Schedule.AsExpandable()
.Where(predicate)
.GroupBy(c => new { c.CustomerID, c.s84_Customer.CustomerName, c.SubdivisionID, c.s84_Subdivision.SubdivisionName, c.LotNumber })
.Select(grouped => new s84_Report_Project_POCO
{
CustomerID = grouped.Key.CustomerID,
CustomerName = grouped.Key.CustomerName,
SubdivisionID = grouped.Key.SubdivisionID,
SubdivisionName = grouped.Key.SubdivisionName,
LotNumber = grouped.Key.LotNumber,
Products = grouped.Select(x => new s84_Report_Project_Product
{
ProductID = x.ProductID,
ProductName = x.s84_Product.ProductName,
ProductDate = x.CustomerExpectedDate,
FieldRepID = x.FieldRepID,
FieldRepName = x.s84_FieldRep.FieldRepName,
InstallerID = x.InstallerID,
InstallerName = x.s84_Installer.InstallerName,
StatusID = x.StatusID,
StatusColor = x.s84_Status.StatusColor,
StatusName = x.s84_Status.StatusName,
Completed = x.Completed
}).ToList()
});
var finalList = groupedData.ToList().Where(x => x.Products.Last().Completed == false).ToList();
List<s84_Report_Project_POCO> lst = finalList.OrderBy(x => x.Products.First().ProductDate).ToList();
return lst;
}
使用(var context=new SalesEntities())
{
var groupedData=context.s84_Schedule.AsExpandable()
.Where(谓词)
.GroupBy(c=>new{c.CustomerID,c.s84_Customer.CustomerName,c.subsectionid,c.s84_subsectionname,c.LotNumber})
.选择(分组=>新s84报告项目POCO
{
CustomerID=grouped.Key.CustomerID,
CustomerName=grouped.Key.CustomerName,
SubsectionId=grouped.Key.SubsectionId,
SubdivisionName=grouped.Key.SubdivisionName,
LotNumber=grouped.Key.LotNumber,
产品=分组。选择(x=>新s84报告项目产品
{
ProductID=x.ProductID,
ProductName=x.s84\U Product.ProductName,
ProductDate=x.CustomerExpectedDate,
FieldRepID=x.FieldRepID,
FieldRepName=x.s84_FieldRep.FieldRepName,
InstallerID=x.InstallerID,
InstallerName=x.s84_Installer.InstallerName,
StatusID=x.StatusID,
StatusColor=x.s84_Status.StatusColor,
StatusName=x.s84_Status.StatusName,
已完成=x.已完成
})托利斯先生()
});
var finalList=groupedData.ToList().Where(x=>x.Products.Last().Completed==false.ToList();
List lst=finalList.OrderBy(x=>x.Products.First().ProductDate).ToList();
返回lst;
}
代码对我来说似乎很好,但看看其中一个日期是如何出问题的
尝试在初始选择上执行订单
var groupedData = context.s84_Schedule.AsExpandable()
.Where(predicate)
.GroupBy(c => new { c.CustomerID,
c.s84_Customer.CustomerName,
c.SubdivisionID,
c.s84_Subdivision.SubdivisionName,
c.LotNumber })
.Select(grouped => new s84_Report_Project_POCO
{
CustomerID = grouped.Key.CustomerID,
CustomerName = grouped.Key.CustomerName,
SubdivisionID = grouped.Key.SubdivisionID,
SubdivisionName = grouped.Key.SubdivisionName,
LotNumber = grouped.Key.LotNumber,
Products = grouped
.Select(x => new s84_Report_Project_Product
{
ProductID = x.ProductID,
ProductName = x.s84_Product.ProductName,
ProductDate = x.CustomerExpectedDate,
FieldRepID = x.FieldRepID,
FieldRepName = x.s84_FieldRep.FieldRepName,
InstallerID = x.InstallerID,
InstallerName = x.s84_Installer.InstallerName,
StatusID = x.StatusID,
StatusColor = x.s84_Status.StatusColor,
StatusName = x.s84_Status.StatusName,
Completed = x.Completed
}).OrderBy(x => x.CustomerExpectedDate).ToList()
});
问题在于
.First()
函数,它返回第一条记录,但不一定按日期顺序返回。如果要按日期对分组的数据进行排序,以便First()
函数返回最近的日期,则需要先对数据进行排序,然后使用First()
函数对结果重新排序:
using (var context = PrimaryConnection.returnNewConnection())
{
var groupedData = context.s84_Schedule.AsExpandable()
.Where(predicate)
.GroupBy(c => new { c.CustomerID, c.s84_Customer.CustomerName, c.SubdivisionID, c.s84_Subdivision.SubdivisionName, c.LotNumber })
.Select(grouped => new s84_Report_Project_POCO
{
CustomerID = grouped.Key.CustomerID,
CustomerName = grouped.Key.CustomerName,
SubdivisionID = grouped.Key.SubdivisionID,
SubdivisionName = grouped.Key.SubdivisionName,
LotNumber = grouped.Key.LotNumber,
Products = grouped
.Select(x => new s84_Report_Project_Product
{
ProductID = x.ProductID,
ProductName = x.s84_Product.ProductName,
ProductDate = x.CustomerExpectedDate,
FieldRepID = x.FieldRepID,
FieldRepName = x.s84_FieldRep.FieldRepName,
InstallerID = x.InstallerID,
InstallerName = x.s84_Installer.InstallerName,
StatusID = x.StatusID,
StatusColor = x.s84_Status.StatusColor,
StatusName = x.s84_Status.StatusName,
Completed = x.Completed
}).Orderby(t => t.CustomerExpectedDate).ToList()
});
var finalList = groupedData.ToList().Where(x => x.Products.Last().Completed == false).ToList();
List<s84_Report_Project_POCO> lst = finalList.OrderBy(x => x.Products.First().ProductDate).ToList();
使用(var context=PrimaryConnection.returnNewConnection())
{
var groupedData=context.s84_Schedule.AsExpandable()
.Where(谓词)
.GroupBy(c=>new{c.CustomerID,c.s84_Customer.CustomerName,c.subsectionid,c.s84_subsectionname,c.LotNumber})
.选择(分组=>新s84报告项目POCO
{
CustomerID=grouped.Key.CustomerID,
CustomerName=grouped.Key.CustomerName,
SubsectionId=grouped.Key.SubsectionId,
SubdivisionName=grouped.Key.SubdivisionName,
LotNumber=grouped.Key.LotNumber,
产品=分组
.选择(x=>新的s84报告项目产品
{
ProductID=x.ProductID,
ProductName=x.s84\U Product.ProductName,
ProductDate=x.CustomerExpectedDate,
FieldRepID=x.FieldRepID,
FieldRepName=x.s84_FieldRep.FieldRepName,
InstallerID=x.InstallerID,
InstallerName=x.s84_Installer.InstallerName,
StatusID=x.StatusID,
StatusColor=x.s84_Status.StatusColor,
StatusName=x.s84_Status.StatusName,
已完成=x.已完成
}).Orderby(t=>t.CustomerExpectedDate).ToList()
});
var finalList=groupedData.ToList().Where(x=>x.Products.Last().Completed==false.ToList();
List lst=finalList.OrderBy(x=>x.Products.First().ProductDate).ToList();
所有SQL查询(以及附加到SQL数据库的Linq查询)都具有随机顺序,除非您对它们进行排序
产品未分类-因此它具有随机顺序。
您可以按Products.First()进行排序,但Products具有随机顺序,因此您的排序也将是随机的
确保在查询中对产品进行了排序,您应该可以
Products = grouped.Select(....)
.OrderBy(x => x.ProductDate)
.ToList()
最后一行…List lst=finalList.OrderBy(x=>x.Products.First().ProductDate).ToList();看起来您只对产品集合的ProductDate中的第一个产品进行排序。这与您在列中使用的值相同吗?。是的,“框架人工开始”、“框架人工粗加工”和“框架人工打孔”都是产品,因此每个日期都是ProductDate。每行的第一个日期应该是x.products.first().ProductDate…显然这个OrderBy没有达到我预期的效果。而且您使用的是lst而不是finalList变量,因为finalList没有排序?您的问题中有很多不相关的代码,我们不知道您是如何使用结果的。您是按每组中的第一个日期排序的-这就是您正在显示的吗?我们无法通知您l、 请发布一个简短但完整的程序来演示这个问题。这个程序必须是
.OrderBy(x=>x.ProductDate)
。请修复它。