Hi fellas

I'm having problem in querying ArrayList with Linq. I'm using VS2005. I installed Linq Preview (May 2006) setup to enable Linq in VS-2005. I created a Linq ASP.NET WebSite Template and wrote the following code.

using System;
using System.Data;
using System.Data.DLinq;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Query;

public partial class Default : System.Web.UI.Page
{
    public class Student
    {
        public String fname;

        public Student(String f)
        {
            this.fname = f;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList al = new ArrayList();
        al.Add(new Student("nlv1"));
        al.Add(new Student("nlv2"));
        al.Add(new Student("nlv3"));

        var query = from Student s in al select s.fname;
        foreach (Student st in query)
        {
            Response.Write(st.fname + "<br/>");
        }
    }
}

I'm getting five errors in that var query line. where have i gone wrong? :'(

Recommended Answers

All 9 Replies

Welcome nlvraghavendra.

query variable represent the collection of strings.

var query = from Student s in al select s.fname;
            string s1 = "";
            foreach (string  st in query)
            {
                s1 = s1 + " " + st;
            }
           ....

Firstly, thanks for your quick response. As you said i made a mistake there. So i corrected it

var query = from Student s in al select s.fname;
        foreach (String st in query)
        {
            Response.Write(st + "<br/>");
        }

But as i mentioned the errors are occuring in that var query line. Its telling as "expected ;" :(

I think you have to install VB 2008 (Framework 3.0,3.5).
var - is C# - 2008 construct.

Try this.

IEnumerable  query = from Student s in al select s.fname;

I did another example with Linq query and is working perfectly in my same VS 05

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
        var lowNums = from n in numbers 
                      where n < 5  
                      select n;
        List<int> l = lowNums.ToList();
        foreach (int a in l)
        {
            Response.Write(a.ToString() + " ");
        }
    }
}

here the var construct is working perfectly. Moreover, while installing Linq preview in VS 2005 it autoatically updated the C# language. Any ideas? only when try to declare a class and try to add its objects in the arraylist and query it linq it is malfuctioning. :'(

Create a list of generic:

List<Student> al = new List<Student>();
           al.Add(new Student("nlv1"));
            al.Add(new Student("nlv2"));
            al.Add(new Student("nlv3"));

I found these two codes working

First :

List<Student> al= new List<Student>();
        al.Add(new Student("nlv1"));
        al.Add(new Student("nlv2"));
        al.Add(new Student("nlv3"));
        var query = from sa in al
                     select sa;
        foreach (Student st in query)
        {
            Response.Write(st.fname + "<br/>");
        }

Second:

ArrayList al= new ArrayList();
        al.Add(new Student("nlv1"));
        al.Add(new Student("nlv2"));
        al.Add(new Student("nlv3"));
        Student sa = new Student();
        var query = from sa in al
                     select sa;
        foreach (Student st in query)
        {
            Response.Write(st.fname + "<br/>");
        }

But in both of them i'm not able to use where clause..

For ex, if i want to fetch the student object where student.fname == "nlv1" how to do that?

when i try to give,

var query = from sa in al
where sa.fname == "nlv1"
select sa;

i'm not getting fname in snippet at all when i give "sa." Hope you understand. Any ideas?

Thanks for your replies! Very much appreciated..

Regards
NLV

var query = from sa in al 
where sa.fname == "nlv1"
select sa.fname;

nope sir/madam

Its not working. The problem is i'm not getting the data members in the snippet at all (even though i declared sa as Student in the previous line)...

Regards
NLV

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.