gpt4 book ai didi

asp.net - 序列化 Linq 对象不起作用

转载 作者:行者123 更新时间:2023-12-01 08:05:43 24 4
gpt4 key购买 nike

使用以下代码:

    Private Sub MakeMeSomeXmlBeforeRyanGetsAngry()

Dim db As New MyDBDataContext

Dim customer = From c In db.Customers Select c

Dim dcs As New DataContractSerializer(GetType(Customer))

Dim sb As StringBuilder = New StringBuilder
Dim writer As XmlWriter = XmlWriter.Create(sb)
dcs.WriteObject(writer, customer)
Dim xml As String = sb.ToString

Response.Write(xml)

End Sub

我正在尝试序列化我的 linq 客户集合。但它一直在扔

Type 'System.Data.Linq.DataQuery`1[MyDB.Customer]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See the Microsoft .NET Framework documentation for other supported types.



我的问题是我已经将 dbml 序列化模式标记为 UniDirectional,当我检查所有 DataContract()> 和 DataMember()> 元素背后的 dbml 代码时。

我不知道如何继续。我曾尝试添加各种 dataloadoptions 并将 deferredloading 设置为 false,但没有运气。

想法?

最佳答案

我认为问题的出现是因为 LINQ 查询仅按需处理,并且不可序列化。尝试序列化由 LINQ 查询(数组、列表、单个项目等)产生的可序列化数据类型

dcs.WriteObject(writer, customer.ToArray)

(我假设您只需要序列化查询的结果,而不是查询本身)

关于asp.net - 序列化 Linq 对象不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2641144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com