我要从表格中提供具有特定字词的那些文件,并且我已经使用了订单,以便那些计数最高的文档首先出现。例如
文件1:这是一所学校。这是我的学校 文件2:这是我们学校的 文件3:我的学校就是这个这个
现在如果我使用
select Document.Id, Document_Word.Location
from Document, Document_Word, word
where Document.Id = Document_Word.Document_Id
and Document_Word.Word_Id = Word.Id
and Word.Word = 'this'
Reault是
我想根据唯一ID的数量通过Descending排序...我实际上需要LINQ查询这个问题
这是我的数据库架构
希望我已经清楚地说明了我的问题......
答案 0 :(得分:2)
以下是使用Entity Framework的示例
using (var context = new MyDbContext())
{
var documentEntities = (from document in context.Documents
join document_word in context.Document_Word on document equals document_word.Document
join word in context.Words on document_word.Word equals word
where word.Word1 == "this" // Filter for word = "this"
group document_word by document_word.Document.Id into documentWordGroup // First group Document_Words by document Id so that we can sort based on the Id count
let IdCount = documentWordGroup.Count() // store the count into a variable IdCount
orderby IdCount descending // here we sort by the IdCount
select documentWordGroup).ToArray() // select the sorted Document_Word groups
.SelectMany(dw => dw); // combine the groups into a single array of Document_Words
//Display the result in the Console output
Console.WriteLine("ID" + "\t" + "Location");
foreach (var document in documentEntities)
{
Console.WriteLine(document.Document.Id + "\t" + document.Location);
}
}
答案 1 :(得分:1)
这是Linq查询。
var res = (from document in Documents
join document_word in DocumentWords on document.Id equals document_word.Document_Id
join word in Words on document_word.WordId equals word.wordId
group document by document.Id
into g
let IdCount = g.Count()
orderby IdCount descending
select new {Key = g.Key, Items = g.Distinct().ToList()}).
SelectMany(x => x.Items).ToList();
按文档ID分组,并按降序排序并返回组。 希望这对你有所帮助。
答案 2 :(得分:0)
也许SQL查询对您或其他人有帮助(也许您可以将其翻译成linq)
首先我们需要计数(ID)。让我们找到它:
SELECT *,count(ID) FROM document GROUP BY ID order by count(ID)
然后我们可以使用文档内连接上表:
SELECT * FROM document
INNER JOIN ( SELECT *,count(ID) FROM document GROUP BY ID order by count(ID) ) y
ON document.ID=y.ID
此代码将返回按不同位置的数量排序的ID。