I have an SQL Server table:
ID | Code | Name
1 | AS | Andrew Smith
2 | RA | Ryan Andrews
3 | ZS | Zach Simmons
I want to find records where the starting of a code or the starting of the first or the last name in the Name column matches with a keyphrase. For instance, if the keyphrase is 'A', it should return:
1 | AS | Andrew Smith
2 | RA | Ryan Andrews
since the Code and First name of Andrew Smith matches with 'A' and the Last name of Andre Ryan matches with 'A'.
The query I have so far is:
var keyphrase = 'aa';
var employees = await (from e in _dbContext.Employee
where EF.Functions.Like(e.Code, $"{keyphrase}%")
orderby e.Code ascending
select e).ToListAsync();
This works well for searching against the Code. But I am not sure how to match the keyphrase against the first and last name in the name column. Again, I do not want to do a Contains but a StartsWith.
I am using EF core 3.1.
Any help is appreciated.
StartWIth
for the last name since it's not at the start. You might want to consider splitting the first and last names into separate columns.