有没有一种方法可以在C#中保存数据集中的对象列表 [英] Is there a way to save a list of objects in dataset in c#

查看:168
本文介绍了有没有一种方法可以在C#中保存数据集中的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在xml中保存日期列表,我想使用数据集来完成任务,我对使用Entity Framework的数据库也是如此。这使我可以使用 event.eventDates.start
访问日期,但是在数据集中我无法实现。

I want to save a list of dates in an xml, I want to use dataset to achieve the task, I do the same to a database using Entity Framework. This allows me to access the dates using event.eventDates.start but in the dataset I cannot achieve it.

   public class Event
    {
        [Key]
        public string id { get; set; }

        public virtual ICollection<Date> eventDates { get; set; }
    }

日期类别

   public class Date
    {
        public DateTime start { get; set; }
        public DateTime end { get; set; }
    }

使用实体框架时,我可以使用<$ c $访问eventDates对象c> event.eventDates.start
我映射了数据集构建器中sql数据库中的数据,其关系看起来像这样

When using entity framework I can access the eventDates Object using event.eventDates.start I mapped the data from the sql database in the dataset builder the relations look like this

我希望xml文件采用这种格式

I want the xml file to be in this format

<?xml version="1.0" standalone="yes"?>
<db xmlns="http://tempuri.org/LocalDB.xsd">
  <Event>
    <id>ID</id>
    <eventdates>
      <date>
         <startdate></startdate>
         <enddate></enddate>
      <date>  
      <date>
         <startdate></startdate>
         <enddate></enddate>
      <date>  
    </eventdates>
  </Event>
</db>

有什么方法可以使用数据集来实现?
我是C#新手,将不胜感激

Is there any way to achieve that using datasets? I'm new to C# any help would be appreciated

推荐答案

如果我理解正确,则只想复制EntityFramework使用数据集的行为。
您可以简单地通过使用适当的表,字段和关系创建DataSet(使用集成的Visual Studio设计器)来实现完全相同的操作:

If i understand correctly, you just want to reproduce EntityFramework's behavior using DataSets. You can achieve exactly same thing simply creating DataSet (using integrated VisualStudio designer) with proper tables, fields and relations:

之后,您可以使用以下代码访问数据:

later on you can access your data using code like this:

var ds = new DataSet1();
var ue = ds.UserEvents.FirstOrDefault();
var ued = ue.GetChildRows("FK_UserEvents_EventDates")
          .Cast<DataSet1.EventDatesRow>();

var date = ued.FirstOrDefault().Date;

接下来要做的是序列化-这很容易:
序列化示例

Next thing to do is serialization - it's quite easy: Serialization example

这篇关于有没有一种方法可以在C#中保存数据集中的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆