如何在LINQ中从逗号分隔的字符串中找到精确匹配的数字。

huangapple go评论60阅读模式
英文:

How to find exact matching number from comma separated string in LINQ

问题

我有一个数据库中的列,它包含类似于"2,3,7,5,6,17"的错误代码,并且我已将其作为字符串,并且我想使用LINQ进行精确匹配数字。

例如,我想要查找匹配的数字7(这个数字是动态的)在这个字符串中,然后它将返回true。但问题是,字符串中也包含17,所以即使7不存在,它也会获取包含17的行。我已经使用了Contains()来实现这一点。请给我提供确切的解决方案,以便我可以返回在这个字符串中获得精确匹配的行。

注:我正在使用LINQ从数据库中提取此字符串。

在这个图像中,您可以看到也提取了数字17行,但它不包含7。我只想要匹配7(精确匹配)。

我已经使用的代码:

List<TableModel> result = from DN in dbContext.Example where DN.ErrorType.Contains(ErrorTypetxt).Select({})

DN.ErrorType是从数据库中提取的完整字符串,即2,3,7,5,6,17

ErrorTypetxt是我想要精确匹配的7

如何解决这个问题?

英文:

I have a column in database, it contains error codes like "2,3,7,5,6,17" and I have taken it as a string and I want to exact match digit using linq.

For example I want to find match 7 (This number is taking dynamically) inside this string then it will return true. But the problem is, string also contain 17 so its fetching row which contains 17 also even if 7 is not present. I have used Contains() for that. Kindly give me the exact solution of it so I can return rows where we got exact match inside this string.

Note: This string I am fetching from the Database using LINQ

Image to show Rows fetched from db

In this image you can see the number 17 row also fetched but it doesn't contain 7. I want only for the 7 (exact match only)

Code that I have used

List&lt;TableModel&gt; result = from DN in dbContext.Example where DN.ErrorType.Contains(ErrorTypetxt).Select({})

DN.ErrorType is a complete string fetched from the db i.e 2,3,7,5,6,17

ErrorTypetxt is a 7 that I want to exact match..

How to resolve this?

答案1

得分: 2

var num = 7;
var start = $"{num},";
var end = $",{num}"
var mid = $",{num},";
var same = $"{num}"

var query =
from dn in source
where
dn.ErrorType.StartsWith(start) ||
dn.ErrorType.Contains(mid) ||
dn.ErrorType.EndsWith(end) ||
dn.ErrorType == same
select dn;

var num = 7;
var mid = $",{num},"

var query =
from dn in source
let modified = "," + dn.ErrorType + ","
where modified.Contains(mid)
select dn;

英文:
var num = 7;
var start = $&quot;{num},&quot;;
var end = $&quot;,{num}&quot;;
var mid = $&quot;,{num},&quot;;
var same = $&quot;{num}&quot;;

var query =
  from dn in source
  where
    dn.ErrorType.StartsWith(start) ||
    dn.ErrorType.Contains(mid) ||
    dn.ErrorType.EndsWith(end) ||
    dn.ErrorType == same
  select dn;

Or

var num = 7;
var mid = $&quot;,{num},&quot;;

var query =
  from dn in source
  let modified = &quot;,&quot; + dn.ErrorType + &quot;,&quot;
  where modified.Contains(mid)
  select dn;

答案2

得分: 0

var expected = "7";
var queryResult = "1,2,17,7";
var splitResult = queryResult.Split(',');
bool result = !string.IsNullOrEmpty(splitResult.FirstOrDefault(s => string.Equals(s, expected)));
//returns true

英文:
var expected = &quot;7&quot;;
var queryResult = &quot;1,2,17,7&quot;;
var splitResult = queryResult.Split(&#39;,&#39;);
bool result = !string.IsNullOrEmpty(splitResult.FirstOrDefault(s =&gt; string.Equals(s, expected)));
//returns true

答案3

得分: -1

请检查以下链接:https://dotnetfiddle.net/80k13h

在第一个评论后更新:
使用以下结构

TestTables
.Where(x => 
    ("," + x.Subjects + ",").Contains(",7,") ||
    (x.Subjects.EndsWith("7") && x.Subjects.Length == 1) ||
    (x.Subjects.StartsWith("7,") && x.Subjects.Length == 2) ||
    (x.Subjects.Contains(",7,") && x.Subjects.Length > 2 && 
        x.Subjects[0] != '7' && x.Subjects[x.Subjects.Length - 1] != '7')
)
.ToList()

这将在LINQPad中生成以下SQL,并提供期望的结果。

如何在LINQ中从逗号分隔的字符串中找到精确匹配的数字。

英文:

You can solve this using split first and then use the contain.
Please check this
https://dotnetfiddle.net/80k13h

Update after the first comment:
use this structure

TestTables
.Where(x =&gt; 
    (&quot;,&quot; + x.Subjects + &quot;,&quot;).Contains(&quot;,7,&quot;) ||
    (x.Subjects.EndsWith(&quot;7&quot;) &amp;&amp; x.Subjects.Length == 1) ||
    (x.Subjects.StartsWith(&quot;7,&quot;) &amp;&amp; x.Subjects.Length == 2) ||
    (x.Subjects.Contains(&quot;,7,&quot;) &amp;&amp; x.Subjects.Length &gt; 2 &amp;&amp; 
        x.Subjects[0] != &#39;7&#39; &amp;&amp; x.Subjects[x.Subjects.Length - 1] != &#39;7&#39;)
)
.ToList()

This generates the following SQL in linqpad as give the expected result.
如何在LINQ中从逗号分隔的字符串中找到精确匹配的数字。

huangapple
  • 本文由 发表于 2023年2月14日 20:37:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定