简体   繁体   中英

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") &&
     !metro.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 ie 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?

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); 

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 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM