我有两张桌子:
tblCategory (ID, CategoryName, ParentCategoryID)
tblFile (ID, Name, CategoryID, USerID, LanguageID, UpadateDateTime,
ActiveDateTime, FilePath)
我想从tblFile表中检索所有数据,其中tblFile表的CategoryID的tblCategory.ParentCategoryID = 1。我想做类似下面的事情:
Select * from tblFile where CategoryID is in
(select ID from tblCategory where ParentCategoryID =1) &&
UploadDateTime >= startDate && UploadDateTime <= endDate
在这里,我想要检索属于特定父类别的所有数据,例如tblCategory中的1。 CategoryID是tblFile的外键,对应于tblCategory的ID。
而且,什么是LINQ声明或实体框架。
答案 0 :(得分:0)
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)))
答案 1 :(得分:0)
假设您的EF模型中存在tblCategories
和tblFiles
之间的FK关系且具有默认命名,您可以在两个表上使用带有过滤器的SelectMany投影:
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