简体   繁体   中英

ADO.NET - Loop through SqlDataReader columns dynamically and assign those values to C# Object

I have an application using which the students can select the colleges they wish to join, in the application we currently give option to select max of 3 colleges and we say 1st preferred, 2nd preferred and 3rd preferred, we have a stored procedure which returns the data and the columns of that result set are as below.

 UserId | FirstName | LastName | Email | Mobile | City |
 FirstPreferredCollegeId | FirstPreferredCollegeName |
 FirstPreferredCollegeGrade | FirstPreferredCollegePincode |
 SecondPreferredCollegeId | SecondPreferredCollegeName |
 SecondPreferredCollegeGrade | SecondPreferredCollegePincode |
 ThirdPreferredCollegeId | ThirdPreferredCollegeName |
 ThirdPreferredCollegeGrade | ThirdPreferredCollegePincode |

If we look at the above result set UserId, FirstName, LastName, Email, Mobile, City are unique where are PreferredCollegeId, PreferredCollegeName, PreferredCollegeGrade, PreferredCollegePinCode are repeating thrice.

My c# model looks like below.

Public Class User
{
    Public int UserId {get;set;}
    Public string FirstName {get;set;}
    public string LastName {get;set;}
    public string Email {get;set;}
    public string Mobile {get;set;}
    public string City {get;set;}
    public List<Choice> Choices {get;set;}
}

public class Choice
{
    public string PreferredCollegeId {get;set;}
    public string PreferredCollegeName {get;set;}
    public string PreferredCollegeGrade {get;set;}
    public string PreferredCollegePincode {get;set;}
}

Now ADO.NET code looks like below, reader is SqlDataReader object.

 objUser.UserId = (string)reader["UserId"];
 objUser.FirstName = (string)reader["FirstName"];
 objUser.LastName = (string)reader["LastName"];
 objUser.Email = (string)reader["Email"];
 objUser.Mobile = (string)reader["Mobile"];
 objUser.City = (string)reader["City"];

 /* Here I need to loop through reader and get College details
 Choice ch = new Choice();

Now I need to loop through the SqlDataReader and get the PreferredCollegeId, PreferredCollegeName, PreferredCollegeGrade, PreferredCollegePincode and dynamically create an object of Choice class and bind these values to the Choice object - how do I achieve this?

Try something like this:

var prefixes = new List<string> {"First", "Second", "Third"};
objUser.Choices = new List<Choice>();
foreach(var prefix in prefixes) 
{
   objUser.Choices.Add(new Choice {
       PreferredCollegeId= (string) reader[prefix + "PreferredCollegeId"],
       PreferredCollegeName = (string) reader[prefix + "PreferredCollegeName"],
       PreferredCollegeGrade  = (string) reader[prefix + "PreferredCollegeGrade"],
       PreferredCollegePincode  = (string) reader[prefix + "PreferredCollegePincode"]
   });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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