1

我正在尝试使用 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>
4

2 回答 2

1

问题是 MongoDB 不能将其作为普通查询来执行。

您不能从多个字段构造一个值,然后将其与通过查询发送的值进行比较:

where (x.FirstName + " " + x.LastName) 

这在 MongoDB 中没有直接的等价物。

您可能会(低效地)执行 JavaScript 来执行连接和比较,或者使用聚合框架(不适用于 LINQ)。

通常,我建议您构建一个包含需要比较的数据的字段(将 first + last 放在一起,可能会更改大小写,删除重音符号等),然后将其与相同的字符串进行比较已执行操作。

如果您确实在运行时组合了字符串,您将无法利用字段上的 MongoDB 索引,因此需要扫描集合中的每个文档。

或者,如果可能,考虑独立搜索firstlast名称与and.

于 2013-10-04T15:30:13.410 回答
-1

不应该是这样的(我也可能是错的:))。

var list =  from x in SomeIQueryable
            where (x => (x.FirstName + " " + x.LastName).StartsWith(searchString))
            select new MyObject
            {
                lastname = x.LastName,
                firstname = x.FirstName
            };
return list.ToList();
于 2013-10-04T15:03:14.533 回答