Sql server 以下sql语句的对应LINQ语句是什么

Sql server 以下sql语句的对应LINQ语句是什么,sql-server,linq,entity-framework,Sql Server,Linq,Entity Framework,我有两张桌子: 我想从tblFile表中检索所有数据,其中tblCategory.ParentCategoryID=tblFile表的CategoryID的1。我想做如下事情: Select * from tblFile where CategoryID is in (select ID from tblCategory where ParentCategoryID =1) && UploadDateTime >= startDate && Upload

我有两张桌子:

我想从tblFile表中检索所有数据,其中tblCategory.ParentCategoryID=tblFile表的CategoryID的1。我想做如下事情:

Select * from tblFile where CategoryID is in 
(select ID from tblCategory where ParentCategoryID =1) && 
UploadDateTime >= startDate && UploadDateTime <= endDate
在这里,我想检索属于特定父类别的所有数据,例如TBL类别中的1。CategoryID是TBL文件的外键,对应于TBL类别的ID


并且,什么是LINQ语句或实体框架。

假设您的EF模型中存在tblCategories和tblFiles之间的FK关系,并且具有默认命名,您可以在两个表上使用带过滤器的投影:

var results = _db.tblFile.Where(x=> x.UploadDateTime >= startDate && UploadDateTime <= endDate && (_db.tblCategory.Where(c=> c.ParentCategoryId == 1).Select(c=> c.Id).ToArray().Contains(x.CategoryID)))
db.tblCategories.Where(cat => cat.ParentCategoryID == 1)
             .SelectMany(cat => cat.tblFiles)
             .Where(file => file.UploadDateTime >= startDate && file.UploadDateTime <= endDate)
更新

我相信您的SQL可以通过连接简化:

Select * 
  from tblFile f join tblCategory cat
       on f.CategoryID = cat.ID
  where cat.ParentCategoryID =1
        and f.UploadDateTime >= @startDate && f.UploadDateTime <= @endDate
Select * 
  from tblFile f join tblCategory cat
       on f.CategoryID = cat.ID
  where cat.ParentCategoryID =1
        and f.UploadDateTime >= @startDate && f.UploadDateTime <= @endDate