简体   繁体   中英

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. Our Project ID's are set up as so:

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.

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

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

It's work for me. But search text must match with ID.

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

You can put variable instead of "0192-10-001" text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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