-1

I am working on MVC project and have a dropdown to fill with Country Name. But I want to block some country names so that they will not reflect in that dropdown. My current code is mentioned below. What should I add in my code if I do not want to show 'Liberia' in that dropdown?

I am posting the code in which my function call is running in Visual Studio

private static void FillCombos(GuestInformationPresenter model)
{
    FillCountryLists(model);
}

/// <summary>
/// Function to fill countries in combos.
/// </summary>
/// <param name="model">GuestInformationPresenter type of object</param>
private static void FillCountryLists(GuestInformationPresenter model)
{
    //I want to add some Linq code here to Block some countries. 
    model.FillCountryLists(ReservationService.RetrieveCountries());
}

public static KeyValuePair<string, string>[] RetrieveCountries()
{
    var countries = from LookupData.CountryRow countryRow in LookupManagerCache.Retrieve().CountryRows
                    orderby countryRow.Name
                    select new KeyValuePair<string, string>(DataField.RetrieveValue(() => countryRow.Code), DataField.RetrieveValue(() => countryRow.Name));

    return countries.ToArray();
}

/// <summary>
/// Function to retrieve collection of Countries.
/// </summary>
/// <returns>collection of countries</returns>
public static KeyValuePair<string, string>[] RetrieveCountries()
{
    return LookupManager.RetrieveCountries();
}

public static Dictionary<string, string> RetrieveCountries()
{
    KeyValuePair<string, string>[] countries = CruiseLookup.RetrieveCountries();
    return countries.ToDictionary<KeyValuePair<string, string>, string, string>(pair => pair.Key, pair => pair.Value);
}

// GuestInformationPresenter
public void FillCountryLists(Dictionary<string, string> countryList)
{
    this.CountryList = countryList;
}
ekad
  • 14,436
  • 26
  • 44
  • 46
Raj
  • 183
  • 3
  • 17

2 Answers2

1

You could also add a where clause to your selector excluding the irrelevant countries from your list.

Something like this:

        var countries = from LookupData.CountryRow countryRow in LookupManagerCache.Retrieve().CountryRows
                        where countryRow.Name != "Liberia"
                        orderby countryRow.Name
                        select new KeyValuePair<string, string>(DataField.RetrieveValue(() => countryRow.Code), DataField.RetrieveValue(() => countryRow.Name));
sydeslyde
  • 135
  • 7
  • I have update my code in question. Can you please check now ? – Raj Sep 11 '15 at 11:43
  • Im afraid your code looks a little mixed up, there are three methods called `RetrieveCountries()`, how can this work at all? Also: have you tried my suggestion like adding a `where` clause to filter out the countries you don't want to show? – sydeslyde Sep 11 '15 at 11:52
  • The method that return countries.ToArray(); is common to retrieve country list but when I want this list to bind GuestInformationPresenter/Controller then I want to block those countries. So the solution you suggested will affect my common requirement where I have to show all countries. – Raj Sep 11 '15 at 12:15
  • Okay, now the fog lifts. Then why don't you just filter the `KeyValuePair` Array in the related controlleraction? – sydeslyde Sep 11 '15 at 12:41
  • It's a simple `Array`. So you only have to make sure to include `using System.Linq;` at the beginning of the file and then you can use Linq to filter the Array like this: `countries = countries.Where(kv => kv.Value != "Liberia").ToArray();` (also i would suggest using the key instead of the value, but thats your decision) – sydeslyde Sep 11 '15 at 12:46
0

as per my understanding your question is about LINQ and you want to use SQL not in clause while select. please refer similar question https://stackoverflow.com/a/433031/5324616

Community
  • 1
  • 1
Pras12ya
  • 1
  • 3