开发者

combo-box fill in asp.net

var metroquery = (from metro in objCommon.Lst_Cities
where (SqlMethods.Like(metro.CityNM, "Mumbai") ||
SqlMethods.Like(metro.CityNM, "Delhi" )||
SqlMethods.Like(metro.CityNM, "Kolkata") ||
SqlMethods.Like(metro.CityNM, "Chennai") || 
SqlMethods.Like(metro.CityNM, "Bangalore") ||
SqlMethods.Like(metro.CityNM, "Pune") ||
SqlMethods.Like(metro.CityNM ,"Ahmedabad") ||
SqlMethods.Like(metro.CityNM , "Hyderbad"))
select metro).ToList();

     var nonmetroquery = (from metro in objCommon.Lst_Cities
     where !metro.CityNM.Contains("Mumbai") &&
     !me开发者_Python百科tro.CityNM.Contains("Delhi") &&
     !metro.CityNM.Contains("Kolkata") &&
     !metro.CityNM.Contains("Chennai") &&
  !metro.CityNM.Contains("Bangalore") &&
    !metro.CityNM.Contains("Pune") &&
    !metro.CityNM.Contains("Ahmedabad") &&
    !metro.CityNM.Contains("Hyderbad")
    select metro).ToList();

I have written these two Linq queries to retrieve the cities and add the first order of cities i.e. Mumbai and others at top. Second query contains non-metro cities which I want to append to the combo.

Basically I want metro cities at top and non metro below metro cities.

I have used the following code for this:

 List<Lst_City> lstCity1 = new List<Lst_City>();


            foreach (var t in metroquery)
            {

                lstCity1 = metroquery;

            }

            System.Threading.Thread.Sleep(200);

            foreach (var t in nonmetroquery)
            {
                lstCity1 = nonmetroquery;
            }

but I am getting only the non metro cities binded to the ComboBox.

Any help o hint?


In your foreach method, you are adding the whole list instead of var t.

Perhaps this would work:

List<Lst_City> lstCity1 = new List<Lst_City>();

            foreach (var t in metroquery)
            {

                lstCity1.Add(t);

            }

            System.Threading.Thread.Sleep(200);

            foreach (var t in nonmetroquery)
            {
                lstCity1.Add(t);
            }


You are assigning the result of nonmetroquery (in the second foreach loop) to your list, replacing the former content of the List.

You should do something like this:

lstCity1.AddRange(metroquery);
lstCity1.AddRange(nonmetroquery); 


You can use SQL level filtration to achieve this using union all

Please refer the below link.

http://dotnetalliance.blogspot.in/2013/02/how-to-select-major-cities-on-top-in-sql.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜