.NET Core 过滤器包含不起作用

发布于 2025-01-11 03:39:49 字数 1747 浏览 0 评论 0原文

好吧,我已经搜索过这个并找到了各种似乎是正确答案的东西,但我似乎无法让它发挥作用。我想我已经很接近了。

我已经让这段代码在我的控制器中工作,我从数据表服务器端 ajax 调用它:

 var shares = await _dataService.GetAllAgencyShares();
            if (!string.IsNullOrEmpty(sortColumn) && !string.IsNullOrEmpty(sortColumnDirection))
            {
                shares = shares.AsQueryable().OrderBy(sortColumn + " " + sortColumnDirection);
                
            }
            if (!string.IsNullOrEmpty(searchValue))
            {
                
                shares = shares.Where(m => m.FirstName == searchValue
                         || m.LastName == searchValue
                         || m.AgencyName == searchValue
                         || m.FormId.ToString() == searchValue);
                
            }
 recordsTotal = shares.Count();
            var data = shares.Skip(skip).Take(pageSize).ToList();
            var jsonData = new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data };
            return Ok(jsonData);

问题是我希望搜索部分

if (!string.IsNullOrEmpty(searchValue))
{
 shares = shares.Where(m => m.FirstName == searchValue
                         || m.LastName == searchValue
                         || m.AgencyName == searchValue
                         || m.FormId.ToString() == searchValue);
}
 recordsTotal = shares.Count();
 var data = shares.Skip(skip).Take(pageSize).ToList();
 var jsonData = new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = 
 recordsTotal, data = data };
 return Ok(jsonData);

找到“类似”searchValue 的任何内容。目前这会找到完全匹配的内容。

我尝试过使用 Contains 但我相信这是布尔值。我发现的大多数东西都在使用 EF 和 IQueryable——我没有使用 EF。我尝试将 IEnumerable 转换为 Queryable ,但似乎没有任何效果。

Ok so I have searched for this and have found all kinds of things that seem to be the right answer but I cannot seem to get this working. I think I am close.

I have gotten this code to work in my controller which I call from Datatable serverside ajax:

 var shares = await _dataService.GetAllAgencyShares();
            if (!string.IsNullOrEmpty(sortColumn) && !string.IsNullOrEmpty(sortColumnDirection))
            {
                shares = shares.AsQueryable().OrderBy(sortColumn + " " + sortColumnDirection);
                
            }
            if (!string.IsNullOrEmpty(searchValue))
            {
                
                shares = shares.Where(m => m.FirstName == searchValue
                         || m.LastName == searchValue
                         || m.AgencyName == searchValue
                         || m.FormId.ToString() == searchValue);
                
            }
 recordsTotal = shares.Count();
            var data = shares.Skip(skip).Take(pageSize).ToList();
            var jsonData = new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data };
            return Ok(jsonData);

The problem is I would like the search part

if (!string.IsNullOrEmpty(searchValue))
{
 shares = shares.Where(m => m.FirstName == searchValue
                         || m.LastName == searchValue
                         || m.AgencyName == searchValue
                         || m.FormId.ToString() == searchValue);
}
 recordsTotal = shares.Count();
 var data = shares.Skip(skip).Take(pageSize).ToList();
 var jsonData = new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = 
 recordsTotal, data = data };
 return Ok(jsonData);

to find anything "like" searchValue. Currently this finds exact matches.

I have tried using Contains but I believe that to be boolean. Most things I found are using EF and use IQueryable--I am not using EF. I have tried to cast IEnumerable as Queryable and nothing seems to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天白云 2025-01-18 03:39:49

您究竟是如何使用 .Contains() 的?

如果像这样实现的话,效果很好;

shares = shares.Where(m => m.FirstName.Contains(searchValue)
                         || m.LastName.Contains(searchValue)
                         || m.AgencyName.Contains(searchValue)
                         || m.FormId.ToString().Contains(searchValue));

How exactly did you use .Contains()?

It works fine if implemented like this;

shares = shares.Where(m => m.FirstName.Contains(searchValue)
                         || m.LastName.Contains(searchValue)
                         || m.AgencyName.Contains(searchValue)
                         || m.FormId.ToString().Contains(searchValue));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文