7
Answers

Linq

Pravin Ghadge

Pravin Ghadge

14y
7.4k
1

Iam using VS 2008
I want to add country in Dropdownlist.
I have used following code:
List<string> Country = GetCountryList();

var sortedCountry = from c
in Country orderby c select c;
// var sortedCountry = from c in Country orderby c select c;
ddlCountry.DataSource = sortedCountry;
ddlCountry.DataBind();
 
But iam facing error on line:var sortedCountry = from c in Country orderby c select c;
I have also used foll. namespaces:
using
System.Collections.Generic;
using
System.Globalization;
using
System.Linq;
Answers (7)
0
Suthish Nair
NA 31.7k 4.6m 14y

check the target framework your project using.
if its 3.0 then change to 3.5 and complie the code.
Accepted
0
Sam Hobbs
NA 28.7k 1.3m 14y
I tested your code; it works for me, therefore it is likely the code that precedes the LINQ statement has an error. The errors are saying that a semicolon is missing, so I would look at the code preceding the LINQ.
0
Subhendu De
NA 3.7k 292.3k 14y
This code should work. Manikavellu is correct.

           IList<string> _country = new List<string>();
            _country.Add("India");
            _country.Add("US");
            _country.Add("China");
            _country.Add("Zimbabwe");

            var query = from c in _country
                        orderby c
                        select c;

            DropDownList1.DataSource = query;
            DropDownList1.DataBind();
0
Pravin Ghadge
NA 2.5k 358.4k 14y

There are 6 errors:
Error 2 ; expected
Error 3 ; expected
Error 4 Invalid expression term 'in'
Error 5 ; expected
Error 6 ; expected
Error 7 ; expected

on line:
var sortedCountry = from c in Country orderby c select c;
 
0
Madhu K
NA 2k 446k 14y
0
Manikavelu Velayutham
NA 9.1k 1.6m 14y

Below code works fine, just try this;

 
List<string> s = new List<string>();
s.Add(
"Manik");
s.Add(
"Babu");
s.Add(
"Velu");
var s1 = from s2 in s orderby s2 select s2;
DropDownList1.DataSource = s1;
DropDownList1.DataBind();
 
 
0
Manikavelu Velayutham
NA 9.1k 1.6m 14y
Can you paste the error ?, It will be easier to provide the solution by seeing the exact error.