简体   繁体   English

LINQ,自定义对象,序列化和对象引用

[英]LINQ, custom objects, serialization and object references

I'm currently using LINQ to SQL for data retrieval and then populating my custom objects with LINQ queries. 我目前正在使用LINQ to SQL进行数据检索,然后使用LINQ查询填充自定义对象。 My custom objects inherit from a base class that uses the IsReference = true attribute in the datacontract. 我的自定义对象继承自在数据合同中使用IsReference = true属性的基类。 I'm then taking my object hierarchy and serializing it to XML which is then sent off to a service. 然后,我采用对象层次结构并将其序列化为XML,然后将其发送给服务。

This works however what's happening in the XML is that the object data is repeated for objects throughout the file. 但这有效,但是XML中发生的事情是在整个文件中对对象重复对象数据。 What I'm wanting in the XML file is that if an object has the same data as another object of the same type (regardless of where it sits in the object tree) I'd like it to have a reference to that first object and not repeat the data. 我在XML文件中想要的是,如果一个对象与另一个相同类型的对象具有相同的数据(无论它在对象树中的位置如何),我希望它具有对该第一个对象的引用。不重复数据。

I'm needing some object tracking and not sure if it can be handled at the LINQ DataContext level or since I'm instantiating my own custom objects if I should track it at that level using something like a hashtable for each object type. 我需要一些对象跟踪,并且不确定是否可以在LINQ DataContext级别上进行处理,或者因为我要实例化自己的自定义对象(如果我应该在该级别上使用每种对象类型的哈希表进行跟踪),则需要进行对象跟踪。

Here's what my classes look like: 这是我的班级样子:

class Section1 { 
  public static MyClass GetClasses(int Id)
  {
    using (DataContext dataContext = new DataContext())
    {
      var result = from items in dataContext.Table
        where item.Id == Id
        select items;

      foreach (var entry in result)
      {
        MyClass myClass = new MyClass();
        myclass.Id = entry.Id;
      }
    }
   return myclass;
  }
}

Later on in my program I'm creating another list of the same type of items as a method in another class and I expect some of the results to be the same as in the first call: 稍后,在我的程序中,我将创建另一个与另一个类中的方法具有相同类型的项的列表,并且我希望某些结果与第一次调用中的结果相同:

Class2 {
  public static MyClass GetItemClasses(int ItemId)
  {
    MyClass myClass = new MyClass();

    using (DataContext dataContext = new DataContext())
    {
      var result = from items in dataContext.Table
          where item.Id == ItemId
          select items;

      foreach (var entry in result)
      {
        MyClass myClass = new MyClass();
        myclass.Id = entry.Id;
      }
    }
   return myclass;
  }
}

Doing this successfully populates all my objects which I then serialize. 这样成功地填充了所有我要序列化的对象。 Any ideas on how I can have references in my XML file for those objects that are repeated? 关于如何在XML文件中为重复的对象提供引用的任何想法?

Thanks for any input. 感谢您的任何投入。

Thought I'd update this as I've found a workable solution. 以为我已经找到了可行的解决方案,因此会对其进行更新。

Since my custom objects are separate from my DataContext and not tracked by it I went ahead and created a lookup table for my objects and I check that table before instantiating a new object. 由于我的自定义对象与DataContext是分开的且未被其跟踪,因此我继续为我的对象创建了一个查找表,并在实例化新对象之前检查了该表。

This has provided me with the references in the XML file that I was after. 这为我提供了我所追求的XML文件中的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM