我正在尝试获取列值并将其转换为格式化字符串值列表,我尝试了以下代码
List<string> _names = (from p in context.RakHolders select
string.Format("{0},{1}", p.Name.Split(',')[0].Trim(), p.Name.Split(',')[1].Trim()))
.ToList();
我的目的是获取名称列表,名称列中逗号之间没有不需要的空格
我在执行此行时遇到异常
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method
'System.String Format(System.String, System.Object, System.Object)' method, and this method cannot be translated into a store expression.
如何使用 LINQ 完成这项工作[获取格式化列值的列表]
最佳答案
List<string> _names = (from p in context.RakHolders select p.Name)
.ToList()
.Select(name => name.Split(','))
.Select(nameSplitted => string.Format("{0},{1}", nameSplitted[0].Trim(), nameSplitted[1].Trim() ))
.ToList();
说明:“string.Format”无法转换为 SQL,因此首先我们需要使用第一个“ToList()”具体化列表(这里我们获取名称列表,它比具体化完整实体高效得多)。将列表存储在内存中,您可以将其转换为您需要的形式。
关于c# - LINQ 选择格式化值作为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40417956/