本文介绍了IQueryable&lt;&gt;序列化 - C#-VS2008-WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我用所需的表构建了我的 *.dbml 文件,并生成了一对多的关系.I built my *.dbml file with the required tables and that generated the relationships, 1 to many.我的 WCF 服务库中的一个方法有这个查询One of my methods in my WCF Service library has this query IQueryable<Client>localClient = from c in db.Clients where c.ClientID.Equals(2612) select c; foreach(Client currentClient in localClient) { //Call serialize method here }客户具有一对多关系的表之一是客户 - 员工One of the table which client has a 1 to many relationships is Client - Employee使用localClient",我想对其进行序列化并返回给调用者.但是它告诉我无法形成 XML 文档.With 'localClient', I want to serialize that and return to the invoker. However it tells me that the the XML document could not be formed.这是内部异常:序列化 TestDB_Public.Employee 类型的对象时检测到循环引用.This is the InnerException:A circular reference was detected while serializing an object of type TestDB_Public.Employee.我的序列化代码 public string Serialize(object o, XmlSerializerNamespaces ns) { try { System.IO.MemoryStream m = new System.IO.MemoryStream(); if (ns != null) serializer.Serialize(m, o, ns); else serializer.Serialize(m, o); m.Position = 0; byte[] b = new byte[m.Length]; m.Read(b, 0, b.Length); return System.Text.UTF8Encoding.UTF8.GetString(b); } catch (Exception ex) { return "Ex = " + ex.ToString(); } }IQueryable 的序列化与 1 对多的关系是不可能的吗?Is serialization of IQueryable<> with 1 to many relationships not possible?推荐答案您不能序列化具有循环关系的对象图:You can't serialise an object graph that has cyclical relationships:class Employee{ Employee Manager; List<Employee> Employees;}var bossMan = new Employee();var emp2 = new Employee{Manager = bossMan}var bossMan.Employees.Add(emp2);如果你现在尝试序列化bossman或emp2,你会得到异常.If you now try to serialise bossman or emp2, you will get the exception. 看看这篇文章,查看循环对象图以获得解决方案.Have a look at this post, check the Cyclic Object Graphs for a solution. 这篇关于IQueryable&lt;&gt;序列化 - C#-VS2008-WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-31 16:10