I'm executing a sql script using:
SqlCommand command = new SqlCommand(sqlQuery, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if(reader.HasRows)
{
while(reader.Read())
{
ViewModel vm = new ViewModel();
vm.Name = reader.GetString(1);
vm.Price = reader.GetDecimal(2);
vm.ReferenceNumber = reader.GetInt32(5);
returnList.Add(vm);
}
}
The above will produce result similar to this:
Name Price RefNumber
----------| --------|-----------
ABC | 93.45 | 1000
esd | 32.32 | 1002
def | 332.32 | 1000
In short, it's possible to have non-unique RefNumber so I want to disguise these number with a string. For example all refNumber with say:
1000 should be called to 'Ref 1' 1002 should be called to 'Ref 2' 1003 should be called to 'Ref 3 and so on.
How do I check this in the SqlDataReader to disguise the name and save to view model?
I've tried something like this:
if(reader.HasRows)
{
while(reader.Read())
{
ViewModel vm = new ViewModel();
vm.Name = reader.GetString(1);
vm.Price = reader.GetDecimal(2);
vm.ReferenceNumber = reader.GetInt32(5);
if(vm.ReferenceNumber == 1000)
{
vm.RefNameAsString = "Ref 1"
}
else if(vm.ReferenceNumber == 1002){
vm.RefNameAsString = "Ref 2"
}
else {
vm.RefNameAsString = "Ref 3"
}
returnList.Add(vm);
}
}
Something like that will produce the desire outcome but the problem is that the RefNumber may be long (100s) so I just want to put these as list and be able to change the name for each distinct reference number?
May be something like this:
foreach (var i in returnList.Select(x => x.ReferenceNumber).Distinct().ToList())
{
//how do I change the name for each unique value?
}