Linq-to-Entities:我的 OrderBy 子句没有排序
Linq-to-Entities: My OrderBy clause isn't ordering
我有一个 Linq 语句,它使用流利和点符号的混合来排序、分组、再次排序,然后对某些实体执行 Skip
/Take
:
(from terms in db.stt_term
where !terms.deleted && languageIDs.Contains(terms.language_id)
join concepts in db.stt_concept
on terms.concept_id equals concepts.id
where !concepts.deleted && concepts.dictionary_id == dictionaryID
select terms).GroupBy(t => t.concept_id)
.Select(grp => new
{
ConceptId = grp.Key,
// the order of the Terms in each grouping should be dictated by the order of the Languages
Terms = grp.OrderBy(t => SqlFunctions.CharIndex(strLangIDs, "_" + t.language_id + "_"))
})
// now order all the groups by the first term in each
.OrderBy(grp => grp.Terms.FirstOrDefault().term_translation)
.Skip(page*conceptsPerPage)
.Take(conceptsPerPage)
好的,如果没有任何背景解释,这可能有点太多了,但我想提请你注意这条线
Terms = grp.OrderBy(t => SqlFunctions.CharIndex(strLangIDs, "_" + t.language_id + "_"))
这是我尝试获得类似 IndexOf
的功能。我之所以求助于此,是因为 Linq-to-Entities 无法识别 IndexOf
调用,而我能找到的最接近的东西是为 linq-to-SQL 提供的函数中的 CharIndex
.
CharIndex
显然需要一个字符串,而我在这里真正想做的是根据 .language_id
属性 的索引位置对 term
实体进行排序整数数组。因此,为了避免没有可用的 IndexOf
,我将整数数组转换为 _
分隔的字符串,然后搜索相应格式的 language_id
.
例如,数组 {15, 1, 3, 4}
变成 "_15_1_3_4_"
,然后我在这个字符串中找到 "_3_"
的索引。 language_id = 15
的条款应排在 lanaguage_id = 1
.
的条款之上
但是,结果显示没有发生任何排序。
如果我将 OrderBy
语句转换为
Terms = grp.OrderBy(t => t.language_id))
然后订购 发生。但是,当我使用我对 SqlFunctions.CharIndex
.
的调用时,并没有发生任何排序。
谁能解释为什么 CharIndex
没有按我预期的那样订购?
你正在用目标反转搜索...SqlFunctions.CharIndex
public static Nullable<int> CharIndex(
string toSearch,
string target
)
颠倒两者。
来自
SqlFunctions.CharIndex(strLangIDs, "_" + t.language_id + "_")
到
SqlFunctions.CharIndex("_" + t.language_id + "_", strLangIDs)
我有一个 Linq 语句,它使用流利和点符号的混合来排序、分组、再次排序,然后对某些实体执行 Skip
/Take
:
(from terms in db.stt_term
where !terms.deleted && languageIDs.Contains(terms.language_id)
join concepts in db.stt_concept
on terms.concept_id equals concepts.id
where !concepts.deleted && concepts.dictionary_id == dictionaryID
select terms).GroupBy(t => t.concept_id)
.Select(grp => new
{
ConceptId = grp.Key,
// the order of the Terms in each grouping should be dictated by the order of the Languages
Terms = grp.OrderBy(t => SqlFunctions.CharIndex(strLangIDs, "_" + t.language_id + "_"))
})
// now order all the groups by the first term in each
.OrderBy(grp => grp.Terms.FirstOrDefault().term_translation)
.Skip(page*conceptsPerPage)
.Take(conceptsPerPage)
好的,如果没有任何背景解释,这可能有点太多了,但我想提请你注意这条线
Terms = grp.OrderBy(t => SqlFunctions.CharIndex(strLangIDs, "_" + t.language_id + "_"))
这是我尝试获得类似 IndexOf
的功能。我之所以求助于此,是因为 Linq-to-Entities 无法识别 IndexOf
调用,而我能找到的最接近的东西是为 linq-to-SQL 提供的函数中的 CharIndex
.
CharIndex
显然需要一个字符串,而我在这里真正想做的是根据 .language_id
属性 的索引位置对 term
实体进行排序整数数组。因此,为了避免没有可用的 IndexOf
,我将整数数组转换为 _
分隔的字符串,然后搜索相应格式的 language_id
.
例如,数组 {15, 1, 3, 4}
变成 "_15_1_3_4_"
,然后我在这个字符串中找到 "_3_"
的索引。 language_id = 15
的条款应排在 lanaguage_id = 1
.
但是,结果显示没有发生任何排序。
如果我将 OrderBy
语句转换为
Terms = grp.OrderBy(t => t.language_id))
然后订购 发生。但是,当我使用我对 SqlFunctions.CharIndex
.
谁能解释为什么 CharIndex
没有按我预期的那样订购?
你正在用目标反转搜索...SqlFunctions.CharIndex
public static Nullable<int> CharIndex(
string toSearch,
string target
)
颠倒两者。
来自
SqlFunctions.CharIndex(strLangIDs, "_" + t.language_id + "_")
到
SqlFunctions.CharIndex("_" + t.language_id + "_", strLangIDs)