0

I'm using the Select2 plugin for searching on my table through LINQ. The issue I'm facing is the multi-select returns me an array and I get an error that I cant convert string[] to string. Here is my view:

<select class="js-example-basic-multiple"name="country[]" multiple="multiple">
  <option value="">Select a country...</option>
  <option value="AF">Afghanistan</option>
  <option value="AX">Åland Islands</option>
  <option value="AL">Albania</option>
</select>

I want on the controller to search records that I select from the dropdown. My controller is:

var model = from r in db.Clients
  orderby r.DateRegister descending
  where (r.DateRegister >= datepicker3 && r.DateRegister <= datepicker2 || datepicker2 == null || datepicker3 == null)
  where r.Name.Contains(FirstName) || FirstName == "" || FirstName == null
  where r.LastName.Contains(LastName) || LastName == "" || LastName == null
  where r.Phone.Contains(Phone) || Phone == "" || Phone == null
  where r.Email.Contains(Email) || Email == "" || Email == null
  where r.PromoCode.Contains(PromoCode) || PromoCode == "" || PromoCode == null
  where r.Brand.Contains(Brand) || Brand == "" || Brand == null
  where r.Rentetiion == Retetion || Retetion == null
  where r.SaleStatus == statos || statos == null
  where r.Desk == desk || desk == null
  where r.workerId == Broker || Broker == null
  where r.Country.Contains(country)||country == null

Where country will be more then 1 item to search.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Eni Spetsi
  • 41
  • 8

2 Answers2

1

1) Suppose this is your Country model.

class Country
{
    public string Name { get; set; }
    public string Code { get; set; }
}

2) And this string[] contains country from your select2.

string[] country = new string[2] { "India", "Shrilanka" };

3) And those are the countries that are already in my database.

List<Country> countries = new List<Country>();
countries.Add(new Country { Name = "India", Code = "IN" });
countries.Add(new Country { Name = "Australia", Code = "AU" });
countries.Add(new Country { Name = "Shrilanka", Code = "SL" });

4) Then I used below query to filtered out countries from my database with above string[] that contains only 2 countries.

var query = (from r in countries
        where country == null || country.Any(c => c.Trim().ToLower().Contains(r.Name.Trim().ToLower()))
        select new
        {
           CountryName = r.Name,
           CountryCode = r.Code
        });


var result = query.ToList();

Trim() => Removes all leading and trailing white-space characters from the current System.String.

ToLower() => Returns a copy of this string converted to lowercase.

Output:

enter image description here

Try once may it help you.

er-sho
  • 9,581
  • 2
  • 13
  • 26
0

Try this,

where r.Country.Contains(c => Country.Contains(c.country) || c.country == null)
johnny
  • 19,272
  • 52
  • 157
  • 259
  • i get this error: Severity Code Description Project File Line Suppression State Error CS1660 Cannot convert lambda expression to type 'string' because it is – Eni Spetsi Aug 28 '18 at 13:40
  • 1
    @Eni, try to import namespace `using System.Linq;` and `using System.Data.Entity;` – er-sho Aug 28 '18 at 13:47
  • same error: S1660 Cannot convert lambda expression to type 'string' because it is not a delegate type – Eni Spetsi Aug 28 '18 at 13:53
  • @EniSpetsi please make sure on ershoaib's comment. It typically can be the fix. Example, https://stackoverflow.com/questions/19197481/cannot-convert-lambda-expression-to-type-string-because-it-is-not-a-delegate-t – johnny Aug 28 '18 at 13:56