简体   繁体   English

实体框架返回所有包含 substring 的项目

[英]Entity Framework return all projects that contain substring

I'm working in a asp.net core site where i'm trying to query our project database through the entity framework.我在 asp.net 核心站点工作,我试图通过实体框架查询我们的项目数据库。 Our Project ID's are set up as so:我们的项目 ID 设置如下:

0192-10-001, 0192-10-001A, 0192-10-001B, 0192-10-001C, 0192-10-001BE, ... 0192-10-001, 0192-10-001A, 0192-10-001B, 0192-10-001C, 0192-10-001BE, ...

for the same project but tracked by letters through separate departments.对于同一个项目,但通过不同部门的信件进行跟踪。 I want to be able to strip out the letters and return all projects that are affiliated by substring (0192-10-001).I'm newer to linq and I'm most likely overthinking this entirely but I'm hoping someone can point me in the right direction.我希望能够去掉字母并返回 substring (0192-10-001) 附属的所有项目。我是 linq 的新手,我很可能完全想多了,但我希望有人能指出我在正确的方向。

I've tried the following queries:我尝试了以下查询:

//This is what I thought would work
return _context.Projects.Where(a => a.Project_ID.Contains(ID));

//Another approach
return _context.Projects.Where(a => a.Project_ID == ID);

//Getting more confused and desperate
return _context.Projects.Where(a => ID.Any(n => a.Project_ID.Contains(ID)));

Let me know if you need any more information!如果您需要更多信息,请告诉我!

If you would like to return List of Projects whose Project_IDs contain ID ("0192-10-001"),just use如果您想返回 Project_IDs 包含ID ("0192-10-001") 的项目列表,只需使用

var list = _context.Projects.Where(p => p.Project_ID.Contains(ID)).ToList();

It's work for me.这对我有用。 But search text must match with ID.但搜索文本必须与 ID 匹配。

var filtered = _context.Projects.Where(x => "0192-10-001".Contains(x.ID)).ToList();

You can put variable instead of "0192-10-001" text.您可以放置变量而不是“0192-10-001”文本。

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

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