我正在尝试使用 LINQ 查询名字 + " " + 姓氏。
这是我尝试过的:
var list = from x in SomeIQueryable
where (x.FirstName + " " + x.LastName).StartsWith(searchString)
select new MyObject
{
lastname = x.LastName,
firstname = x.FirstName
};
return list.ToList();
对于 MongoDB,这会返回一个异常:
System.NotSupportedException: Unable to determine the serialization information for the expression: ((x.FirstName + " ") + x.LastName).
at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.GetSerializationInfo(Expression node, Dictionary`2 serializationInfoCache)
at MongoDB.Driver.Linq.Utils.BsonSerializationInfoHelper.GetSerializationInfo(Expression node)
at MongoDB.Driver.Linq.PredicateTranslator.BuildStringQuery(MethodCallExpression methodCallExpression)
at MongoDB.Driver.Linq.PredicateTranslator.BuildMethodCallQuery(MethodCallExpression methodCallExpression)
at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
at MongoDB.Driver.Linq.PredicateTranslator.BuildOrElseQuery(BinaryExpression binaryExpression)
at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
at MongoDB.Driver.Linq.SelectQuery.BuildQuery()
at MongoDB.Driver.Linq.SelectQuery.Execute()
at MongoDB.Driver.Linq.MongoQueryProvider.Execute(Expression expression)
at MongoDB.Driver.Linq.MongoQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at <snip - my code>
看起来查询解析器无法确定如何从中正确构建查询。我试图通过使用 Concat() LINQ 扩展来帮助它,但这也不起作用:
var list = from x in SomeIQueryable
where (string.Concat(x.FirstName, " ", x.LastName)).StartsWith(searchString)
select new MyObject
{
lastname = x.LastName,
firstname = x.FirstName
};
return list.ToList();
它不喜欢它:
System.ArgumentException: Unsupported where clause: String.Concat(x.FirstName, " ", x.LastName).StartsWith("a").
at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
at MongoDB.Driver.Linq.PredicateTranslator.BuildOrElseQuery(BinaryExpression binaryExpression)
at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
at MongoDB.Driver.Linq.SelectQuery.BuildQuery()
at MongoDB.Driver.Linq.SelectQuery.Execute()
at MongoDB.Driver.Linq.MongoQueryProvider.Execute(Expression expression)
at MongoDB.Driver.Linq.MongoQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at <snip - my code>