带有子查询的Ravendb Select返回完整对象,而不是子查询项
本文关键字:查询 对象 返回 Ravendb Select | 更新日期: 2023-09-27 18:06:08
异常
Cannot deserialize JSON object into type 'System.String'.
控制器
var products = session.Query<Package>().Select(m => new PackageViewModel() {
Description = m.Description,
Id = m.Id,
Title = m.Title,
Urls = m.Items.Select(g => g.Url).ToArray() //string[] Urls
});
当我将Urls
更改为object
并进行分配时,Urls
被设置为m.Items
的完整值,而不是URL列表。
将上面的内容改为下面的内容就可以了。
var products = session.Query<Package>().ToList();
var viewModel = products.Select(m => new PackageViewModel() {
Description = m.Description,
Id = m.Id,
Title = m.Title,
Urls = m.Items.Select(g => g.Url).ToArray()
}).ToList();
这真的不是问题,但我想知道我是不是做错了什么
完整堆栈跟踪
[JsonSerializationException: Cannot deserialize JSON object into type 'System.String'.]
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:417
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:223
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:208
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IWrappedCollection wrappedList, JsonReader reader, String reference, JsonArrayContract contract) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:719
Newtonsoft.Json.Serialization.<>c__DisplayClass1.<CreateAndPopulateList>b__0(IList l, Boolean isTemporaryListReference) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:665
Newtonsoft.Json.Utilities.CollectionUtils.CreateAndPopulateList(Type listType, Action`2 populateList) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Utilities'CollectionUtils.cs:544
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulateList(JsonReader reader, String reference, JsonArrayContract contract) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:653
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String reference) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:446
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:225
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueProperty(JsonReader reader, JsonProperty property, Object target, Boolean gottenCurrentValue, Object currentValue) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:198
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonReader reader, Object target) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:552
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, String id) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:1028
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulateObject(JsonReader reader, JsonObjectContract contract, String id) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:858
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:396
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:223
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:208
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'Serialization'JsonSerializerInternalReader.cs:120
Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'JsonSerializer.cs:421
Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) in d:'Development'Releases'Json'Working'Src'Newtonsoft.Json'JsonSerializer.cs:413
Raven.Client.Document.SessionOperations.QueryOperation.DeserializedResult(RavenJObject result) in c:'Builds'raven'Raven.Client.Lightweight'Document'SessionOperations'QueryOperation.cs:173
Raven.Client.Document.SessionOperations.QueryOperation.Deserialize(RavenJObject result) in c:'Builds'raven'Raven.Client.Lightweight'Document'SessionOperations'QueryOperation.cs:139
System.Linq.WhereSelectListIterator`2.MoveNext() +110
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +327
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
Raven.Client.Document.SessionOperations.QueryOperation.Complete() in c:'Builds'raven'Raven.Client.Lightweight'Document'SessionOperations'QueryOperation.cs:111
Raven.Client.Document.AbstractDocumentQuery`2.GetEnumerator() in c:'Builds'raven'Raven.Client.Lightweight'Document'AbstractDocumentQuery.cs:551
Raven.Client.Linq.RavenQueryInspector`1.GetEnumerator() in c:'Builds'raven'Raven.Client.Lightweight'Linq'RavenQueryInspector.cs:94
Raven.Client.Linq.RavenQueryInspector`1.System.Collections.IEnumerable.GetEnumerator() in c:'Builds'raven'Raven.Client.Lightweight'Linq'RavenQueryInspector.cs:99
System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +61
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +1380
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +194
System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat) +26
System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat) +74
System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj) +6
Mvc.Areas.Api.Controllers.PackagesController.List() in PackagesController.cs:38
lambda_method(Closure , ControllerBase , Object[] ) +96
linq解析器不够聪明,无法处理此投影。它可以进行简单的投影,但嵌套的东西非常复杂,不受支持。您可以使用LuceneQuery API(要加载的字段为"Items,Url"(