Hi @Jerry Lipan ,
First, when using the contains methods like the above, it will check whether the clientid contain the specified element. Based on the code, for example the CliensId is 8
, then using the above code, it will check whether it contains the "('8', '6', '1', '18')"
, and the result is clear, it can't find any items. More detail information about contains method, see Enumerable.Contains Method and Queryable.Contains Method.
How to fix that ? ClientsId variable parameter can be
('2', '8', '6', '1', '13', '18')
('2', '8')
('1', '13', '18')
('6', '1')
If I need to change variable parameter format, please let me know
Then, based on your description, I assume you will receive an input value which might contain the above string value. After that, you could use Regex Matches to fill all clientsid from the input string, and put them into an array or list, then you can use the contains method to filter data.
You can refer the following sample code:
//test data
var _SFAProject = new List<SFAProject>()
{
new SFAProject(){ Id=1, TenderName="A", ClientsId= 6},
new SFAProject(){ Id=2, TenderName="B", ClientsId= 18},
new SFAProject(){ Id=3, TenderName="C", ClientsId= 13},
new SFAProject(){ Id=4, TenderName="D", ClientsId= 13},
new SFAProject(){ Id=5, TenderName="E", ClientsId= 8},
};
//the input value
var inputclientsid = "('8', '6', '1', '18')";
//use regex to find all clientsid
string pattern = @"\b[0-9]+\b";
Regex rgx = new Regex(pattern);
//find all clients id and store them into a list.
var ids = rgx.Matches(inputclientsid).Cast<Match>().Select(c => c.Value).ToList();
//filter data based on the input clientsid
var filterresult = (from s in _SFAProject
where ids.Contains(s.ClientsId.ToString())
select s).ToList();
//you can also use this method
var filterresult2 = (from s in _SFAProject
where ids.Any(c=> c == s.ClientsId.ToString())
select s).ToList();
Then, the result like this:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion