0

I'm trying to check does the values of (string[]) contains in string. When I run the code only first value of string separated with comma is check. I want to check all values of string.

 // Model property (User)
 // Example 1002, 5000, 3300 (Skills ids)
 public string userSkillsId { get; set; }

 // Index filtering with multiple values(select2)
 // Example 1002, 5000 (Skills ids to check if contains in userSkillsId)
 public string SkillsId { get; set; } 

 public async Task<IActionResult> Index(ContestantViewModel viewModel, int currentPage)
 {
     string[] skills = viewModel.SkillsId?.Split(",");

     if (!(viewModel.SkillsId is null))
        entity = entity.Where(x => ((IList)skills).Contains(x.userSkillsId));

     viewModel.Result = result;
  }
dontbyteme
  • 1,221
  • 1
  • 11
  • 23
WingmanColt
  • 21
  • 1
  • 6
  • 1
    maybe spaces are the issue, try Split(",").Select(p => p.Trim(); – Victor Fialkin Nov 30 '20 at 12:47
  • "first value of string separated with comma is check. " - how do you see this happens? You mean if you need to check if `"abc"` is in array, then `Contains` returns `true` if `"abc"` is the first element of the array, and `false` if it's the second, third and so on ? – E. Shcherbo Nov 30 '20 at 12:52
  • `((IList)skills)` Why are you doing that cast? – mjwills Nov 30 '20 at 12:56
  • Can you talk us through why `userSkillsId` is modelled as a string rather than an array / list? `Trim` will fix your immediate issue, but my greater concern is your modelling is wrong. – mjwills Nov 30 '20 at 12:58
  • I'am here: List parts = viewModel.SkillsId?.Split(',').Select(p => p.Trim()).ToList(); if (!(parts is null)) entity = entity.Where(x => parts.Contains(x.userSkillsId)); when property string x.userSkillsId == 1992,1003,5555 .Contains get only 1992. I want to check others too. If i convert string x.userSkillsId to ArrayList framework will create another table for that property, I do not want it. – WingmanColt Nov 30 '20 at 15:36

1 Answers1

0

use separator as char

string[] skills = viewModel.SkillsId?.Split(',');

Beso
  • 1,176
  • 5
  • 12
  • 26