【发布时间】:2009-08-07 19:48:00
【问题描述】:
我有一个 LINQ 查询,它在多个字段中搜索字符串(使用正则表达式)。我想根据在哪个字段中找到文本对结果进行排序。
目前我有这个:
var results = from i in passwordData.Tables["PasswordValue"].AsEnumerable()
where r.IsMatch(i.Field<String>("Key").Replace(" ","")) ||
r.IsMatch(i.Field<String>("Username").Replace(" ","")) ||
r.IsMatch(i.Field<String>("Other").Replace(" ",""))
orderby i.Field<String>("Key"),
i.Field<String>("Other"),
i.Field<String>("Username")
select i;
我希望首先在 Key 中找到匹配项,然后在 Other 中找到匹配项,然后在 Username 中找到匹配项。如果可能,同时匹配 Key 和 Other 的匹配项应先于仅匹配 Key 的匹配项。
我目前的代码首先基于 Key 进行排序,因此如果在 Other 上找到匹配但 Key 以 A 开头,它将在 Key 以 Z 开头的 Key 上找到匹配之前排序。
提前谢谢,我认为这不是一个很难的问题,但我只是不知道如何做到这一点,因为我是 LINQ 的新手。
【问题讨论】: