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?
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#.
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.
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.
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.
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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With