Linq查询组按重量计算

时间:2018-06-13 16:11:17

标签: c# asp.net linq

AllWordIDsAndWeightings是搜索查询中出现的字词的Dictionary<int, double>唯一ID,以及相应的权重。

给出以下查询:

returnedObjectIDs = new List<int>(db.WordObjectMaps
    .Where(c =>
        c.ForObjectTypeID == TopicObjectTypeID
        && AllWordIDsAndWeightings.Select(w=> w.Key).ToList().Contains(c.WordID)
    )
    .GroupBy(c => c.ForObjectID)
    .Select(c => c.Key)
);

我想修改它以便:

group by语句包含每个单词的word frequency * word weighting之和。然后我可以通过此值降序排序并获取最高n条记录。

这可能吗?

1 个答案:

答案 0 :(得分:0)

returnedObjectIDs = new List<int>(db.WordObjectMaps
    .Where(c =>
        c.ForObjectTypeID == TopicObjectTypeID
        && AllWordIDsAndWeightings.ContainsKey(c.WordID)
    )
    .Select(c => new { Word = c, Weight = AllWordIDsAndWeightings[c.WordID])
    .GroupBy(c => x.Word.Value * c.Weight) // replace with your condition
    .Select(c => c.Key)
);