Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of LINQ and ArrayList

Tags:

c#

linq

I've recently used LINQ

In the following code:

ArrayList list = new ArrayList();
var myStrings = list.AsQueryable().Cast<string>();

What is the AsQueryable for? I know Cast creates a type-safe collection, and ArrayList is deprecated.

I've got a friend who says he needs the AsQueryable combined with ArrayList. I'm trying to understand why, but I can't see why AsQueryable is needed.

Is he wrong?

like image 281
PRASHANT P Avatar asked Feb 21 '11 23:02

PRASHANT P


People also ask

Can we use LINQ on ArrayList?

LINQ with ArrayListYou don't need to specify array size unlike traditional array and the size grows as you will add more element into it. However, it is slower than Array but it is more useful when you work with collection. Here, in this example, we will learn how to process data in ArrayList using LINQ C#.

What is the main purpose of LINQ?

LINQ that stands for Language Integrated Query (pronounced as “link”) is a . NET language extension that supports data retrieval from different data sources like XML document, databases and collections. It was introduced in the . NET 3.5 framework.

What are the advantages of using LINQ?

Advantages of Using LINQLINQ offers a common syntax for querying any type of data sources. Secondly, it binds the gap between relational and object-oriented approachs. LINQ expedites development time by catching errors at compile time and includes IntelliSense & Debugging support. LINQ expressions are Strongly Typed.

What is LINQ and how it is useful in Sharepoint?

LINQ is a feature of the programming languages C# and Microsoft Visual Basic . NET. Compilers are included with Visual Studio. LINQ adds a SQL-like syntax and vocabulary to each of the languages, which can be used to query data sources.


1 Answers

You do not need the call to AsQueryable(). Queryables only make sense when a LINQ query (expressed in C#) needs to be converted to another domain language (such as SQL). In your case since you are working with LINQ to Objects (you are operating on an array list) this is not needed.

You can call the Cast<T>() method directly on the list instance. Another choice would be to start with a strongly-typed collection such as List<T>.

like image 115
marcind Avatar answered Sep 22 '22 14:09

marcind