| | |
Problem with Linq query
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 11
Reputation:
Solved Threads: 0
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.
I'm getting five errors in that var query line. where have i gone wrong?
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.
C# Syntax (Toggle Plain Text)
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?
Welcome nlvraghavendra.
query variable represent the collection of strings.
query variable represent the collection of strings.
C# Syntax (Toggle Plain Text)
var query = from Student s in al select s.fname; string s1 = ""; foreach (string st in query) { s1 = s1 + " " + st; } ....
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Jun 2009
Posts: 11
Reputation:
Solved Threads: 0
Firstly, thanks for your quick response. As you said i made a mistake there. So i corrected it
But as i mentioned the errors are occuring in that var query line. Its telling as "expected ;"
C# Syntax (Toggle Plain Text)
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.
var - is C# - 2008 construct.
Try this.
C# Syntax (Toggle Plain Text)
IEnumerable query = from Student s in al select s.fname;
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Jun 2009
Posts: 11
Reputation:
Solved Threads: 0
I did another example with Linq query and is working perfectly in my same VS 05
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.
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
List<Student> al = new List<Student>(); al.Add(new Student("nlv1")); al.Add(new Student("nlv2")); al.Add(new Student("nlv3"));
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Jun 2009
Posts: 11
Reputation:
Solved Threads: 0
I found these two codes working
First :
Second:
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
First :
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
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
C# Syntax (Toggle Plain Text)
var query = from sa in al where sa.fname == "nlv1" select sa.fname;
Failure is not fatal, but failure to change might be. - John Wooden
![]() |
Similar Threads
- LINQ query \ LAMBDA expression - complex requirement (C#)
- Problem with MySQL Query (Database Design)
- [LINQ, C#]Problem with autogenerated DataClasses.designer.cs (ASP.NET)
- Problem execute the Query (PHP)
- Problem regarding mysql query: it is displaying duplicate contents (MySQL)
- Query class problem (PHP)
- Problem with query. (MS SQL)
- query problem (MySQL)
- problem with lengthy query (Java)
Other Threads in the C# Forum
- Previous Thread: C#.net
- Next Thread: convert to pdf
| Thread Tools | Search this Thread |
.net access algorithm array asp barchart bitmap box broadcast buttons c# check checkbox client column combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development display draganddrop drawing encryption enum equation event excel file form format formbox forms formupdate function gdi+ httpwebrequest image index input install java label linux list listbox mandelbrot math mouseclick mysql networking operator packaging parse path photoshop picturebox pixelinversion post powerpacks programming radians regex remote remoting reporting richtextbox robot server sleep socket sql statistics stream string table text textbox thread time timer transform treeview update usercontrol validation visualstudio webbrowser wfa windows winforms wpf xml






