DZone
Database Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > Convert Table Data to List Using Reflection

Convert Table Data to List Using Reflection

In this blog, learn how to convert table data into an organized list using Reflection and LINQ. C# and MS SQL code are also used for this process.

Thiruppathi Rengasamy user avatar by
Thiruppathi Rengasamy
CORE ·
Jun. 13, 17 · Database Zone · Tutorial
Like (2)
Save
Tweet
6.65K Views

Join the DZone community and get the full member experience.

Join For Free

In this blog, learn how to convert table data into list data using Reflection and LINQ. You can use the data passing from the server side to the client side.

Reflection

You can use Reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from existing object, and invoke its methods or access fields and properties. If you are using attributes in your code, Reflection enables you to access them.

It will provide object assemblies, modules, and types.

Model Properties

Create a simple variable in the model class in which you can use whatever data types that you want to.

public class Logininfo {
 public int UserID {
  get;
  set;
 }
 public string UserName {
  get;
  set;
 }

}
Public Class LogininfoList {
 public List < Logininfo > ListLogininfo {
  get;
  set;
 }
}

Table Column

Create a table — but note that you must set the column name as the model properties. If there is a mismatch, then the name reflection will not work.

CREATE TABLE logininformation 
  ( 
     userid   INT NULL, 
     username VARCHAR NULL 
  ) 

Conversion

If ADO.NET is used in this situation, then after the execution, the data will fill the dataset.

SqlCommand command = new SqlCommand(sp, connection) {
 CommandType = CommandType.StoredProcedure, CommandTimeout = connection.ConnectionTimeout
};
connection.Open();
command.Parameters.AddRange(prms);
DataSet dataSet = new DataSet();
(new SqlDataAdapter(command)).Fill(dataSet);
command.Parameters.Clear();
return dataSet;

Convert the table into a list by using the extension ToList<> .

LogininfoList obj= new LogininfoList();  
Obj. ListLogininfo= ds.Tables[1].ToList<Logininfo>().ToList();

Reflection has created an instance of a property and data transfer from the table to the list that is provided below in the code.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
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;
}

Note: This scenario is useful when it comes to using ADO.NET.

Conclusion

In this blog, we have converted table data into a list using Reflection. If you have any questions, please tell me through the comments section.

Database Data (computing) Convert (command)

Published at DZone with permission of Thiruppathi Rengasamy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing Between REST and GraphQL
  • APIs Outside, Events Inside
  • Take Control of Your Application Security
  • SDLC Vs STLC: What's the Difference?

Comments

Database Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo