I have C# Lambda linq query as follow:
public IQueryable<CtArticleDetail> GetArticleDetailsByTagNames(string tagNames)
{
return db.CtArticleTags.Where(m => tagNames.Contains(m.CtTag.Name)
&& m.CtArticleDetail.ExpirationDate > DateTime.Now
&& m.CtArticleDetail.ArticleStatusId == (int)ArticleStatus.Published)
.Select(m => m.CtArticleDetail).OrderBy(x => x.LiveDate).Distinct();
}
This returns slightly wrong data list because of “Contains”. The parameter actually comes this format: EG ( “AterSale,GeneralSale”). The result list contains other sale type articles.EG (“Sale” or “ImportedSale”).
The reuslt list should be exact list as parameter passed. Can anyone pls advice ?
var mustContain = new[] {"A", "B");
var foos = new List<Foo>(){
new Foo1 {Tags = "A"},
new Foo1 {Tags = "B"},
new Foo2 {Tags = "A"},
new Foo2 {Tags = "C"}
};
I just need Foo1 if mustcontain is A,B
If i understand you correctly
tagNamesis a string which is a list of tagNames separated by comma. Then you could useString.Split:According to your comment
You can use
Enumerable.All, here’s a Linq-To-Objects example which you can hopefully convert to Linq-To-Entities: