Using LINQ to query for non-generic types

Source: Internet
Author: User

LINQ enables querying of generic objects or objects that implement the IEnumerable interface. However, non-generic collections such as ArrayList do not implement the IEnumerable interface. Next, let's take a look at how to use LINQ to query Non-generic collections in this case.

C#

1public class Cars
2{
3public string CarMake { get;set;}
4public string CarModel { get; set; }
5public int Year { get; set; }
6}
7
8
9class Program
10{
11static void Main(string[] args)
12{
13    ArrayList carList = new ArrayList();
14    carList.Add(new Cars
15        {
16            CarMake="BMW", CarModel="BMW Art", Year=1978
17        });
18    carList.Add(new Cars
19        {
20            CarMake = "BMW", CarModel = "Coupe", Year = 1982
21        });
22    carList.Add(new Cars
23        {
24            CarMake = "Renault", CarModel = "Alpine", Year = 1972
25        });
26    carList.Add(new Cars
27        {
28            CarMake = "Porsche", CarModel = "Maisto", Year = 1976
29        });
30
31    var carQuery = from car in carList
32                   where car.CarMake == "BMW"
33                   select car;
34}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.