I have a list of string
List<string> totalTags =new ({"Test1","test2","Test3","test4","Test5"});
I want to search the table Tags and find list of following Name and make a list for their associated Ids The Tag Table has two fields ,Id and Name. like
Id Name
==============
1 Test1
2 Test4
and so on
var IdList= _context.Tags.Where(t2 => !totalTags .Any(t1 => t2.Name.Contains(t1))).Select(p=>p.Id).ToList();
and want to have
IdList ={"1","2",....} (the Id of the rest)
but it doesn't work
CodePudding user response:
I think you might want to compare t1 == t2.Name
in Any
becasue t2.Name
is a string instead of list, otherwise that will compare by char
.
var IdList = _context.Tags.Where(t2 => totalTags.Any(t1 => t1 == t2.Name)).Select(p => p.Id).ToList();
CodePudding user response:
If you want to select all ids
from _context.Tags
where tag.Name
should present in totalTags
list, then try below
var IdList = _context.Tags`
.Where(x => totalTags.Contains(x.Name)) //No `!` operator
.Select(x => x.Id)
.ToList();
CodePudding user response:
You should use Contains
method like
var idsList = _context.Tags
.Where(t=> totalTags.Contains(t.Name))
.Select(t=> t.Id)
.ToList()