将数据表映射到列表 的 LINQ

发布于 2024-10-03 09:42:20 字数 209 浏览 3 评论 0 原文

我刚刚发现 LINQ,所以请对我进行全面的了解! :-)

所以!我有一个数据层,它为我提供数据表,我想将它们转换为对象列表。这些对象在特定层DTO(数据传输对象)中定义。

如何将数据表的每一行映射到对象并将所有对象放入列表中? (今天我“手动”逐个字段地进行) 可以用LINQ吗?我听说过 LINQ2Entities?我说得对吗?

感谢帮助初学者理解...

I just discover LINQ so be comprehensive with me please! :-)

So! I have a Data-tier who provide me datatables and i want to convert them into lists of objects. These objects are defined in a spécific layer DTO (Data transfer Objects).

How can I map every rows of my datatable into objects and put the all objects into a list? (today i make it "manually" field after field)
Is it possible with LINQ? I've heard about LINQ2Entities? am i right?

Thanks to help a beginner to understand...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜雨飘雪 2024-10-10 09:42:20

如果对象不是太复杂,您可以使用它:

public static class DataTableExtensions
{
   public static IList<T> ToList<T>(this DataTable table) where T : new()
   {
      IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
      IList<T> result = new List<T>();

      foreach (var row in table.Rows)
      {
         var item = CreateItemFromRow<T>((DataRow)row, properties);
         result.Add(item);
      }

      return result;
   }

   private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
   {
       T item = new T();
       foreach (var property in properties)
       {
           property.SetValue(item, row[property.Name], null);
       }
       return item;
   }
}

现在您可以编写:var list = YourDataTable.ToList()

您可以在这里阅读:http://blog.tomasjansson。 com/convert-datatable-to-generic-list-extension/

这是对上一个问题的答案:将 DataTable 转换为 C# 中的通用列表

编辑: 我应该补充一点,这不是 linq,而是 DataTable我写的。此外,它还遵循约定,即您要映射的对象中的属性与数据表中的属性具有相同的名称。当然,这可以扩展为读取属性上的属性,或者方法本身可以采用可用于执行映射的简单的 Dictionary 。您还可以使用一些功能来扩展它,这些功能采用可用于排除某些属性的params string[] exceptProperties

If the objects is not too complex you can use this:

public static class DataTableExtensions
{
   public static IList<T> ToList<T>(this DataTable table) where T : new()
   {
      IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
      IList<T> result = new List<T>();

      foreach (var row in table.Rows)
      {
         var item = CreateItemFromRow<T>((DataRow)row, properties);
         result.Add(item);
      }

      return result;
   }

   private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
   {
       T item = new T();
       foreach (var property in properties)
       {
           property.SetValue(item, row[property.Name], null);
       }
       return item;
   }
}

With that in place you can now write: var list = YourDataTable.ToList<YourEntityType>().

You can read about it here: http://blog.tomasjansson.com/convert-datatable-to-generic-list-extension/

And it is an answer to a previous question: Convert DataTable to Generic List in C#

EDIT: I should add that this is not linq, but some extension methods to DataTable I wrote. Also, it is working with the convention that the properties in the object you're mapping with has the same name as in the DataTable. Of course this could be extended to read attributes on the properties or the method itself could take a simple Dictionary<string,string> that could be used to do the mapping. You could also extend it with some functionality that take a params string[] excludeProperties that could be used to exclude some of the properties.

简单 2024-10-10 09:42:20

I would suggest reading about The ADO.NET Entity Framework. It supports what you're asking, and the link should provide you with sufficient information and examples :)

There are also plenty of tutorials out there about the topic to get you started.

尐偏执 2024-10-10 09:42:20

最好检查该列是否存在于行中以另一种方式进行映射,它将引发异常,在我的情况下,我有两个对象,其中一个比另一个具有相同名称和数据类型的属性更多

  private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
   {
       T item = new T();
       foreach (var property in properties)
       {  
           if (row.Table.Columns.Contains(property.Name))
           {
           property.SetValue(item, row[property.Name], null);
           }
       }
       return item;
   }

it's better to Check if the column exist in the row to do the mapping another way it will throw an exception, in my case I have two objects one of them have more proprieties than the other with the same name and data type

  private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
   {
       T item = new T();
       foreach (var property in properties)
       {  
           if (row.Table.Columns.Contains(property.Name))
           {
           property.SetValue(item, row[property.Name], null);
           }
       }
       return item;
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文