What does ToList() method work in reality? [Resolved]

Posted by Kumarkrishna184 under ADO.NET on 12/13/2015 | Points: 10 | Views : 358 | Status : [Member] | Replies : 6
What does ToList() method work in reality? Why it is used? Please explain following code...

var dinfo=obj.depts.ToList();

Thanks and Regards,
Krishna Kumar



Responses

Posted by: Sheonarayan on: 12/13/2015 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
.ToList();

ToList() method creates a new collection of the object on which it is called. In this case, assuming depts will have collection of dept object so dinfo variable will be IEnumerable of dept (ie. collection of dept objects).

Using EF and LINQ, many times you will not be able to directly return obj.depts as collection so you will need to convert it to .ToList() and then return to the view. ToList() method actually extracts the data out of object (in this case depts) and put into a new collection.

Once you have the ToList() collection, that collection will not be attached with the db object and you will be able to perform all LINQ operations on that object.

Hope this helps.



Regards,
Sheo Narayan
http://www.dotnetfunda.com

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/13/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Kumarkrishna184, in addition to what Sheo Sir replied, I would like to add some more usage of ToList()

a) It helps to convert an anonymous type to a collection
b) If we want to add new objects to a named collection e.g
dt.AsEnumerable().Select(dr=>dr[0]).Tolist() .Foreach(i=>lst.Add(i));
where dt is a Datatable , dr is the datarows and lst is a string collection . In this example the Tolist() is converting the datarows to a generic types and adding those to the list of strings.

c) Converts an IQuerable to IEnumerable

But in your example var dinfo=obj.depts.ToList(); I don't think that .toList() conversion is needed as obj.depts is already a collection and without .ToList() also it will work.

hope that helps

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/13/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Kumarkrishna184 Sir, thanks for your kind words. However, we must not forget

" I do not know what I may appear to the world, but to myself I seem to have been only like a boy playing on the seashore, and diverting myself in now and then finding a smoother pebble or a prettier shell than ordinary, whilst the great ocean of truth lay all undiscovered before me. "
- Sir Isaac Newton

In another way,

“There is no end to learning, but there are many beginnings” - Tim Johnson

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kumarkrishna184 on: 12/13/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks sir...

Thanks and Regards,
Krishna Kumar

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/13/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Kumarkrishna184, you are welcome Sir

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kumarkrishna184 on: 12/13/2015 [Member] Starter | Points: 25

Up
0
Down
Because of you both (Niladri Biswas and Sheo Narayan) Sir, I am Becoming Master in Dotnet Thanks again Sir.....

Thanks and Regards,
Krishna Kumar

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response