Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert from IList<T> to List<T>

Posted on 2010-11-14
4
Medium Priority
?
694 Views
Last Modified: 2013-12-17
Dear All,


Sometimes this simple conversion works: (in C# )


List<T> result = ilistobject as List<T>;



But sometimes it may give me NULL result and I have to use this:


List<T> result = ilistobject.OfType<T>().ToList<T>();
 

How come?

Thanks a lot!
0
Comment
Question by:chenyuhao88
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 600 total points
ID: 34133315
Those are NOT the same operations. In the first, you are attempting to cast an object to a List<T> and if the object is not compatible with a List<T> you are given NULL instead. In the second, you are filtering your list for only objects of type T (OfType<T>) and then returning a list of those objects (ToList).
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 800 total points
ID: 34134039
System.Collections.Generic.IList<T> is an Interface which is implemented in following System classes
List<T>, SortedList<TKey,TValue>,System.Collections.ObjectModel.Collection<T>, System.Collections.ObjectModel.ReadOnlyCollection<T>

So if we are having a different type of collection object in ilistobject which is not compatible with List<T> then


List<T> result = ilistobject as List<T>;
result will be null.

and  ilistobject.OfType<T>().ToList<T>(); will filter the ilistobject based on type T.

so if we are having an IList<Object>


            IList<object> ilistobject = new object[] { "abcd", 2, 3 };

            List<String> result = ilistobject.OfType<string>().ToList<string>();

we will get abcd in result.

0
 
LVL 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 600 total points
ID: 34140241
IList<T> supports IEnumerable interface, just pass ilistobject to the list constructor.
IList<string> ilistobject = new string[] { "abcd", "test", "pass" };
List<String> result = new List<string>(ilistobject);

You can add an extra check depending on how your overall code is structured:
            if (ilistobject is IList<string>)
            {
                List<String> result = new List<string>(ilistobject);
            }
       
0
 

Author Closing Comment

by:chenyuhao88
ID: 34161865
Thanks guys!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question