简体   繁体   中英

LINQ expression for exact match of part of string in Entity framework

I tried to get list of MasterDto using following query.But it didnt return expected result.So i want to get exact match results of given pattern.

 // Department Code like AB,ABC,BC,...etc.
    string jobNumberPrefixPattern= departmentCode; 

 MasterDto reportDto = unitOfWork.GetRepository<MasterDto>().LinqQuery
                                (m => m.CreateUserid.ToUpper() == userName.ToUpper()
                                      && m.JobNumberTemp.StartsWith(jobNumberPrefixPattern))
                                 .OrderByDescending(m => m.MasterPk)
                                 .FirstOrDefault();

JobNumberTemp is string field and its created based on departmentCode and next value of number.samples are like as follows.

  • AB-0001
  • AB-0002
  • ABC-0001

Here I used string.StartsWith , instead of using this is there any suggestions to do ?

Your query is fine. The value you're using for jobNumberPrefixPattern is the problem. ABC starts with AB, if your pattern is AB then I returning ABC values is expected. If you want AB and not ABC you should use AB- as your jobNumberPrefix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM