C# 将SQL语句转换为Linq
我正在尝试将以下内容转换为C#中的LINQ to SQL语句。谁能帮我一把吗?基本上,我的表记录了所有更改的历史记录,这样每个种子批的创建日期max date就是最新的记录,也是要显示的正确记录C# 将SQL语句转换为Linq,c#,sql,linq,C#,Sql,Linq,我正在尝试将以下内容转换为C#中的LINQ to SQL语句。谁能帮我一把吗?基本上,我的表记录了所有更改的历史记录,这样每个种子批的创建日期max date就是最新的记录,也是要显示的正确记录 SELECT reports.* FROM [dbo].[Reports] reports WHERE reports.createdDate IN ( SELECT MAX(report_max_dates.createdDate) FR
SELECT
reports.*
FROM
[dbo].[Reports] reports
WHERE
reports.createdDate
IN (
SELECT
MAX(report_max_dates.createdDate)
FROM
[dbo].[Reports] report_max_dates
GROUP BY
report_max_dates.Lot
)
到目前为止,这就是我所拥有的
var result = (from report in db.Reports
where report.createdDate == (from report_max in db.Reports
group report_max by report_max.Lot into report_max_grouped
select report_max_grouped).Max()
select report);
我不知道如何获取所有报告的MAX
日期,以及如何在report.createdDate上执行IN
语句
编辑
如果我有一个包含多个报告的单独标题,我现在将如何对该语句建模。例如,我有报告l,m,n,x,y,z。报告l m n的标题“hello”通过外键reportList_Id链接到它们,x y c的标题“再见”以相同的方式链接
基本上,我现在需要将所有报告放入以下对象中
public class ReportRoot : DbContext {
public DbSet<ReportList> reportList {get;set;}
}
public class ReportList {
public int Id {get;set;}
public string status {get;set;}
public List<ReportItem> {get;set;}
}
public class ReportItem {
public int Id {get;set}
public string title {get;set;}
public List<Report> {get;set;}
}
public class Report {
public int Id {get;set;}
public string Lot {get;set;}
public DateTime createdDate {get;set;}
}
坦斯克,
Dman我可能不太明白,但基本上,您想要每个不同批次的最后创建日期吗? 如果是,那么
db.Reports
.GroupBy(r => r.Lot)
.Select(g => g.OrderByDescending(x => x.createdDate).FirstOrDefault());
或者像你一样做(但我不确定这是最简单的方式)
编辑
你的例子不太清楚(为了清楚起见,我稍微更改了一下名称)
对于代码优先,您应该有类似的东西(想法是一样的)
课堂报告
public class Report {
public int Id {get;set;}
public int Lot {get;set;}
public Datetime CreatedDate {get;set;}
public virtual Category Category {get;set;} //Navigation property
}
类别
public class Category {
public int Id {get;set;}
public string Title {get;set;}
public virtual IList<Report> ReportList {get;set;} //Navigation property
}
为什么呢您的查询看起来只会发回一条记录。对于每个批次,可以有多个条目,因此该特定批次的最大日期就是我需要的每个批次的条目。没有返回结果,因为它抱怨First只能用作最终查询条目。@DMan它是createdDate排序的每个组(批次)的第一个。您可能应该尝试使用FirstOrDefault()而不是First。。。但它会返回一个列表。不是一张唱片。这也和预期的一样。非常感谢,我需要使用FirstOrDefault()。如果我有一张在这张表上面的表,会发生什么。ie我有一个reportList表(因为每个报告都属于一个标题),但一个报告可能有多个标题。我如何对它进行建模,以便返回一个只有这种性质的报告的报告列表?它们在reportList\u id列中作为外键链接的位置。@DMan您使用的是linq到实体(代码优先、模型优先、数据库优先)还是linq到sql?这完全符合预期。谢谢。虽然您更改了它,使其使用from,但无论哪种方式。
var maxDates = Reports.GroupBy(r => r.Lot)
.Select(x => x.Max(g => g.createdDate).ToList();
var result = db.Reports.Where (m => maxDates.Contains(m.createdDate));
public class Report {
public int Id {get;set;}
public int Lot {get;set;}
public Datetime CreatedDate {get;set;}
public virtual Category Category {get;set;} //Navigation property
}
public class Category {
public int Id {get;set;}
public string Title {get;set;}
public virtual IList<Report> ReportList {get;set;} //Navigation property
}
public class ReportList {
public string Title {get;set;}
public List<Report> ReportList {get;set;}
}
db.Reports
.GroupBy(r => r.Lot)
.Select(g => g.OrderByDescending(x =x.createdDate).FirstOrDefault())
.GroupBy(m => m.Category.Id)
.Select(g => new ReportList {
Title = g.FirstOrDefault().Category.Title,
ReportList = g.OrderBy(x => x.Lot)
});