简体   繁体   English

LINQ表达式,用于实体框架中部分字符串的精确匹配

[英]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. 我试图使用以下查询获取MasterDto列表。但它没有返回预期结果。所以我想获得给定模式的完全匹配结果。

 // 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. JobNumberTemp是一个字符串字段,它是基于departmentCode和JobNumberTemp下一个值创建的。 JobNumberTemp如下。

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

Here I used string.StartsWith , instead of using this is there any suggestions to do ? 在这里,我使用string.StartsWith ,而不是使用它,有什么建议吗?

Your query is fine. 您的查询很好。 The value you're using for jobNumberPrefixPattern is the problem. 您为jobNumberPrefixPattern使用的值就是问题。 ABC starts with AB, if your pattern is AB then I returning ABC values is expected. ABC以AB开头,如果您的模式是AB,则期望返回ABC值。 If you want AB and not ABC you should use AB- as your jobNumberPrefix. 如果您想要AB而不是ABC,则应使用AB-作为jobNumberPrefix。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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