简体   繁体   English

在 LINQ Where 子句不起作用中使用 String.Split

[英]using String.Split in a LINQ Where clause not working

I have below LINQ query:我有以下 LINQ 查询:

List<string> paths = (from an in modelDb.MyTable
                            where an.Id == 0 &&
                                  an.fileName.Split('_')[0] == "19292929383833abe838ac393" &&
                                  an.fileName.Split('_')[1].ToUpper() == "FINANCE" &&
                                  an.fileName.ToUpper().Contains("SIGNED")
                            select an.filePath).ToList();

... which is throwing below error on run-time: ...在运行时抛出以下错误:

Entity Framework 'ArrayIndex' is not supported in LINQ to Entities

fileName field in the LINQ query is a column in MyTable of string data type and contains strings like: LINQ 查询中的 fileName 字段是 MyTable 中字符串数据类型的列,包含如下字符串:

8845abd344ejk3444_FINANCE_SIGNED.pdf
4565abd34ryjk3454_FINANCE_UNSIGNED.pdf
477474jkedf34dfe4_MARKETING_UNSIGNED.pdf

and so on...等等...

As stays in the message split will be not supported in the linq to sql and if you want to get result without additional methods, using Contains() should give you required result由于 linq 到 sql 不支持消息拆分中的停留,如果您想在没有其他方法的情况下获得结果,使用 Contains() 应该会给您所需的结果

List<string> paths = (from an in modelDb.MyTable
        where an.Id == 0 &&
                an.fileName.Contains("19292929383833abe838ac393") &&
                an.fileName.Contains("FINANCE") &&
                an.fileName.ToUpper().Contains("SIGNED")
        select an.filePath).ToList();

Another way is to load all in memory and do split after that, eg另一种方法是在 memory 中加载所有内容,然后进行拆分,例如

List<string> temp = (from an in modelDb.MyTable
                            where an.Id == 0  
                                  an.fileName.ToUpper().Contains("SIGNED")
                            select an.filePath).ToList();

paths = temp.Where(an =>
     an.fileName.Split('_')[0] == "19292929383833abe838ac393" &&
     an.fileName.Split('_')[1].ToUpper() == "FINANCE").ToList();  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM