[英]how use like in sql on linq c#
this not work这行不通
string[] DATE_Group = { "2020", "2021" }; //changing values
var query = _context.c10
.Where(u => DATE_Group.Any(s => u.ON_DATE.Contains(s)))
.ToList()
.Select(u => new
{
User_NumberID = u.User_NumberID.ToString(),
}).ToList();
I use this u.ON_DATE.Contains("2020")
and work but not list or array only one value我使用这个u.ON_DATE.Contains("2020")
并且工作但不列出或仅列出一个值
in sql SELECT * from C10 where ON_DATE like '%2020%' or ON_DATE like '%2021%'
and in ON_DATE column content like this 1-3-2023 1-1-2021 5-1-2020在 sql SELECT * from C10 where ON_DATE like '%2020%' or ON_DATE like '%2021%'
and in ON_DATE column content like this 1-3-2023 1-1-2021 5-1-2020
this error when used enter image description here使用时出现此错误 在此处输入图像描述
将以下代码用于您的 linq where 语句。
var myVar=context.Students.where(x=>x.Name.Contains("John")).SingleOrDefault();
I think this is a better way to get the data:我认为这是获取数据的更好方法:
var from = new DateTime(2020,1,1);
var to = new DateTime(2022,1,1);
var query = _context.c10
.Where(u => u.ON_DATE > from && u.ON_DATE < to )
...
EF Core cannot use Any
with local collections (with small excpetion), only Contains
is supported. EF Core 不能将Any
与本地集合一起使用(具有较小的 excpetion),仅支持Contains
。
I would suggest to use function FilterByItems .我建议使用功能FilterByItems 。 And use in query:并在查询中使用:
string[] DATE_Group = { "2020", "2021" }; //changing values
var query = _context.c10
.FilterByItems(DATE_Group, (u, s) => u.ON_DATE.Contains(s), true)
.Select(u => new
{
User_NumberID = u.User_NumberID.ToString()
})
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.