我正在运行以下查询:(请参阅https://learn.microsoft.com/en-us/sql/relational-databases/search/query-with-full-text-search?view=sql-server-ver16)
SELECT KEY_TBL.RANK, FT_TBL.*
FROM dbo.Forums AS FT_TBL
INNER JOIN FREETEXTTABLE(dbo.Forums, Title,'{searchTerm}') AS KEY_TBL
ON FT_TBL.Id = KEY_TBL.[KEY]
ORDER BY KEY_TBL.RANK DESC当我在中运行它时,这是很好的,但是当我在API中运行它时,我没有得到排名。总是0。
这是我的模型:
public class Forum : BaseModel
{
public ApplicationUser? ForumAuthor { get; set; }
public string ForumAuthorId { get; set; } = "";
[Required (AllowEmptyStrings = false,ErrorMessage = "A Title is required")]
public string? Title { get; set; }
public List<Post>? Posts { get; set; }
[NotMapped]
public int RANK { get; set; }
}注意,级别有一个[NotMapped]属性。我这么做是因为我在论坛的桌子上没有排名字段。
因此,我决定创建一个没有ForumViewModel属性的秩字段的[NotMapped]。
以下是我的疑问:
List<ForumViewModel> forums = await _context.Forums.FromSqlRaw(forumSql).Select(x => new ForumViewModel()
{
ForumAuthor = x.ForumAuthor,
ForumAuthorId = x.ForumAuthorId,
Posts = x.Posts,
RANK = x.RANK,
Title = x.Title,
}).ToListAsync();结果是一样的。等级= 0。
如果删除[NotMapped]属性,则得到有效数字大于0的秩。所以我认为[NotMapped]属性是问题所在。
级别由FREETEXTTABLE返回。因此,我使用DbContext查询我的论坛表,它加入了FREETEXTTABLE。此表返回针对论坛表标题字段中所有文本的搜索词的级别。请看我在这篇文章开头放的链接。
如何获得排名,而不必不必要地将Rank字段添加到Forum表中。
编辑:解决了,我想出来了。针对这种情况有一个数据注释:
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public int RANK { get; set; }发布于 2022-10-29 23:07:29
解决了!我想通了。对于这种情况,有一个数据注释:生成列
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public int RANK { get; set; }发布于 2022-10-29 21:49:40
这是如何在林克获得军衔的方法。您需要按降序顺序排列,然后分组数量。参见下面我创建了一个值的字典。
int[] quantities = { 49, 49, 41, 30, 17, 35, 25, 24, 14, 12 };
Dictionary<int, int> rankDictionary = quantities
.OrderByDescending(x => x)
//skip zero
.Select((x, i) => new { quantity = x, index = i + 1 })
.GroupBy(x => x.quantity, y => y.index)
.ToDictionary(x => x.Key, y => y.Min(z => z));https://stackoverflow.com/questions/74248986
复制相似问题