Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq到实体:我的OrderBy子句不是';t排序_C#_Linq_Entity Framework_Linq To Sql_Linq To Entities - Fatal编程技术网

C# Linq到实体:我的OrderBy子句不是';t排序

C# Linq到实体:我的OrderBy子句不是';t排序,c#,linq,entity-framework,linq-to-sql,linq-to-entities,C#,Linq,Entity Framework,Linq To Sql,Linq To Entities,我有一个Linq语句,它混合使用fluent和点符号来排序、分组、再次排序,然后对某些实体执行跳过/获取: (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.del

我有一个Linq语句,它混合使用fluent和点符号来排序、分组、再次排序,然后对某些实体执行
跳过
/
获取

(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 + "_"))
这是我尝试实现类似于
索引的功能。我之所以求助于此,是因为LINQtoEntities不识别
IndexOf
调用,我能找到的最接近的东西是为LINQtoSQL提供的函数中的
CharIndex

CharIndex
显然需要一个字符串,而我在这里真正想要做的是根据实体的
位置对
项进行排序。language\u id
属性在整数数组中进行索引。因此,为了避免没有可用的
索引,我将整数数组转换为一个
分隔字符串,然后搜索相应格式的
语言id

例如,数组
{15,1,3,4}
变成
“\u 15\u 1\u 3\u 4”
,然后我在这个字符串中找到索引,比如说,
“\u 3\u 4
。对于
language\u id=15
的术语,应按照
lanaguage\u id=1
的顺序排列在术语上方

然而,结果表明没有发生排序

如果我将该
OrderBy
语句转换为

Terms = grp.OrderBy(t => t.language_id))
然后,订单确实发生了。但是当我使用对
SqlFunctions.CharIndex
的调用时,不需要进行排序


有人能解释一下为什么CharIndex没有像我期望的那样排序吗?

您正在将搜索结果与目标进行反向


您使用EntityFramework和LINQtoSQL的可能性接近0%。。。这两种方法中的哪一种?尝试使用SQL探查器查看生成的查询。我要说的是,问题是你不能有两种(一种是外部的,一种是内部的)。我认为SQL Server只会尊重最外部的部分我不确定我是否理解引用EntityFramework和Linq to SQL时出现问题的原因-我正在使用EntityFramework进行查询,并引用为Linq-to-SQL构建的SQL函数命名空间中的方法。如果使用,则其描述为
此函数只能出现在**Linq-to-Entities**查询中。
,但我可以在发出的SQL中看到它的存在:
CAST(CHARINDEX(''u 1'u 3'u 4'u 15'u',N''u'+CAST([Extent3].[language\u id]作为nvarchar(max))+N作为int)作为[C1]
完美!这很有效。但请允许我大胆地指出,微软对这两个参数的描述可能写错了。例如,“toSearch:要搜索的字符串表达式”和“target:要查找的字符串表达式”。你可以理解为什么我把它们搞错了,对吧?@awj这些句子肯定写得很糟糕。事实上,我检查了CHARINDEX页面:)
CHARINDEX(expressionToFind,expressionToSearch[,start\u location])
Ok,仅供将来参考,以防其他人对这些参数感到困惑,CHARINDEX引用()列出并正确描述了这些参数,而SqlFunctions.CHARINDEX引用()似乎将其描述分配给了错误的参数。
public static Nullable<int> CharIndex(
    string toSearch,
    string target
)
SqlFunctions.CharIndex(strLangIDs, "_" + t.language_id + "_")
SqlFunctions.CharIndex("_" + t.language_id + "_", strLangIDs)