Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Which is the best way to implement following two methods in my code


C#
class Sample
{
public string Name {get; set;}
public int ID {get; set;}
}

C#
//Method 1
var list =(from row in ds.Tables[0].AsEnumerable()
                                         select new Sample
                                         {
                                             Name= row.Field<string>("Name"),
                                             ID= row.Field<int>("ID")
                                         }
                                          ).ToList();


C#
//Method 2
List<Sample> sampleList = new List<Sample>(); 
while(sqlDataReader.Read())
{
Sample s1 = new Sample();
s1.Name = (string)sqlDataReader["Name"];
s1.ID = (int)sqlDataReader["ID"];
sampleList.Add(s1);
}


both codes can able to generate generic list object but,
in the first method i am using SqlDataAdapter and then convert the dataset to list using LINQ
In the second method, i am using SqlDataReader and convert the result as list

I want to know which the right way of using these two technique in my code.
I thought Method 2 is optimized way... Is it right?

Regards,
--Sj
Posted
Updated 11-Jun-13 3:33am
v2

It depends.

Don't combine technologies that are meant to replace each-other.
Method 1 is LINQ, but it isn't LINQ to SQL, because you use several explicit casting and that over dataset objects - actually this is an existing approach called LINQ to DataSet[^] - but as I see, this was meant only for transition.
I suggest you use either DataSet approach or LINQ to SQL, or Entity Framework.
Avoid using the integer indexer over the tables collection.

DataReader can be a good approach if the schema is simple, and you don't need bidirectional access - reporting is a good example. It consumes less resources and it is fast.
 
Share this answer
 
Comments
codeninja-C# 11-Jun-13 9:33am    
Hi, If i am using linq for converting dataset result, it may occupy memory so, it will reduce the performance.
but if i am using datareader there is no wastage of local memory so it might increase the performance
please clarify my understanding is true or not...
Zoltán Zörgő 11-Jun-13 9:38am    
True. DataReader is consuming less resources. But you should choose the approach that is most appropriate for the whole project's needs not only a small part of it - and be consistent and consequent.
Method1 is faster. Linq is having capability of handling memory. And easy to manage.
In case you want to utilize available cores in your system linq is the best option. you can easly parallalize.

And one more important thing is your code is not executed yet, until you consume variable list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900