您好,如何在 where
和 select
语句中的 EF
查询中使用 substring
?
我的查询不计入要执行的数据库中的任何记录
但在数据库中有 13359 条记录
我的问题在下面
string subautoassettype, subautocat, subautocat1 ;
string autoidstring;
autocat = cmbcategory.Text;
autocat1 = cmbcategory1.Text;
autocat2 = cmbcategory2.Text;
subautoassettype = autocat.Substring(0, 3);
subautocat = autocat1.Substring(0, 3);
subautocat1 = autocat2.Substring(0, 3);
autoidstring = subautoassettype + subautocat + subautocat1 + "-";
var varmaxidcheck = cnx.item_master.Where(c => c.Item_ID.Substring(0, 9) == autoidstring)
.Select(gd => new { gd.Item_ID }).ToList();
if (varmaxidcheck.Count > 0)
{
foreach (var it11 in varmaxidcheck)
{
string kij = it11.Item_ID;
}
}
示例输出预期:
cnx.item_master(where c=> c.FixBooChe-0013346 == FixBooChe-).Select(fg=>fg.Item_ID);
请帮我解决这个问题
我还包含了 SQL 查询
select max(substring(Item_ID,11,6)) from item_master where substring(Item_ID,1,9) = 'FixEleCre';
最佳答案
使用Contains
而不是 ==
:
var varmaxidcheck = cnx.item_master.Where(c => c.Item_ID.Substring(0, 9)
.Contains(autoidstring))
.Select(gd => new { gd.Item_ID })
.ToList();
因为您的 Where
子句中可能没有完全匹配的字符串。
关于c# - 如何在 where 和 select 语句中的 ef 查询中使用子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44546105/