Problem with Linq query

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 11
Reputation: nlvraghavendra is an unknown quantity at this point 
Solved Threads: 0
nlvraghavendra nlvraghavendra is offline Offline
Newbie Poster

Problem with Linq query

 
0
  #1
Jun 26th, 2009
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.

  1. using System;
  2. using System.Data;
  3. using System.Data.DLinq;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Web;
  8. using System.Web.Security;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Web.UI.HtmlControls;
  13. using System.Query;
  14.  
  15. public partial class Default : System.Web.UI.Page
  16. {
  17. public class Student
  18. {
  19. public String fname;
  20.  
  21. public Student(String f)
  22. {
  23. this.fname = f;
  24. }
  25. }
  26.  
  27. protected void Page_Load(object sender, EventArgs e)
  28. {
  29. ArrayList al = new ArrayList();
  30. al.Add(new Student("nlv1"));
  31. al.Add(new Student("nlv2"));
  32. al.Add(new Student("nlv3"));
  33.  
  34. var query = from Student s in al select s.fname;
  35. foreach (Student st in query)
  36. {
  37. Response.Write(st.fname + "<br/>");
  38. }
  39. }
  40. }

I'm getting five errors in that var query line. where have i gone wrong?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 468
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Problem with Linq query

 
0
  #2
Jun 26th, 2009
Welcome nlvraghavendra.

query variable represent the collection of strings.

  1. var query = from Student s in al select s.fname;
  2. string s1 = "";
  3. foreach (string st in query)
  4. {
  5. s1 = s1 + " " + st;
  6. }
  7. ....
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 11
Reputation: nlvraghavendra is an unknown quantity at this point 
Solved Threads: 0
nlvraghavendra nlvraghavendra is offline Offline
Newbie Poster

Re: Problem with Linq query

 
0
  #3
Jun 26th, 2009
Firstly, thanks for your quick response. As you said i made a mistake there. So i corrected it

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

But as i mentioned the errors are occuring in that var query line. Its telling as "expected ;"
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 468
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Problem with Linq query

 
0
  #4
Jun 26th, 2009
I think you have to install VB 2008 (Framework 3.0,3.5).
var - is C# - 2008 construct.

Try this.
  1. IEnumerable query = from Student s in al select s.fname;
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 11
Reputation: nlvraghavendra is an unknown quantity at this point 
Solved Threads: 0
nlvraghavendra nlvraghavendra is offline Offline
Newbie Poster

Re: Problem with Linq query

 
0
  #5
Jun 26th, 2009
I did another example with Linq query and is working perfectly in my same VS 05

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

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 468
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Problem with Linq query

 
0
  #6
Jun 26th, 2009
Create a list of generic:
  1. List<Student> al = new List<Student>();
  2. al.Add(new Student("nlv1"));
  3. al.Add(new Student("nlv2"));
  4. al.Add(new Student("nlv3"));
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 11
Reputation: nlvraghavendra is an unknown quantity at this point 
Solved Threads: 0
nlvraghavendra nlvraghavendra is offline Offline
Newbie Poster

Re: Problem with Linq query

 
0
  #7
Jun 26th, 2009
I found these two codes working

First :
  1. List<Student> al= new List<Student>();
  2. al.Add(new Student("nlv1"));
  3. al.Add(new Student("nlv2"));
  4. al.Add(new Student("nlv3"));
  5. var query = from sa in al
  6. select sa;
  7. foreach (Student st in query)
  8. {
  9. Response.Write(st.fname + "<br/>");
  10. }

Second:
  1. ArrayList al= new ArrayList();
  2. al.Add(new Student("nlv1"));
  3. al.Add(new Student("nlv2"));
  4. al.Add(new Student("nlv3"));
  5. Student sa = new Student();
  6. var query = from sa in al
  7. select sa;
  8. foreach (Student st in query)
  9. {
  10. Response.Write(st.fname + "<br/>");
  11. }

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 11
Reputation: nlvraghavendra is an unknown quantity at this point 
Solved Threads: 0
nlvraghavendra nlvraghavendra is offline Offline
Newbie Poster

Re: Problem with Linq query

 
0
  #8
Jun 26th, 2009
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 468
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Problem with Linq query

 
0
  #9
Jun 26th, 2009
  1. var query = from sa in al
  2. where sa.fname == "nlv1"
  3. select sa.fname;
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 11
Reputation: nlvraghavendra is an unknown quantity at this point 
Solved Threads: 0
nlvraghavendra nlvraghavendra is offline Offline
Newbie Poster

Re: Problem with Linq query

 
0
  #10
Jun 27th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC