gpt4 book ai didi

Linq 按标签列表过滤帖子

转载 作者:行者123 更新时间:2023-12-02 09:24:26 25 4
gpt4 key购买 nike

public class LogItemTag
{
public int ID { get; set; }
public string Tagname { get; set; }
}

var logItemTags = new List<LogItemTag>();
logItemTags.Add(new LogItemTag { ID = 1, TagName = "red" });
logItemTags.Add(new LogItemTag { ID = 1, TagName = "green" });
logItemTags.Add(new LogItemTag { ID = 3, TagName = "blue" });
logItemTags.Add(new LogItemTag { ID = 3, TagName = "red" });
logItemTags.Add(new LogItemTag { ID = 4, TagName = "green" });
logItemTags.Add(new LogItemTag { ID = 6, TagName = "white" });
logItemTags.Add(new LogItemTag { ID = 7, TagName = "red" });
logItemTags.Add(new LogItemTag { ID = 7, TagName = "green" });

var listOfTagsToFilterOn = new List<string> {"red", "green" };

//I need a list of ids 1 and 7 only.

//this query produces 6 ids - 1,1,3,4,7,7
var query = logItemTags.Where(lit => listOfTagsToFilterOn.Contains(lit.TagName)).Select(lit => lit.ID).ToList();

最佳答案

使用Contains扩展方法:

var tags=new List<string>(){"red", "green"};// the collection of tags you want to get the ids
var query=LogItemTagList.Where(l=>tags.Contains(l.Tagname)).Select(l=>l.ID);

更新 1

我认为这就是您想要实现的目标:

var query=LogItemTagList.Where(l=>tags.Contains(l.Tagname))
.GroupBy(l=>l.ID)
.Where(g=>g.Count==tags.Count)
.Select(g=>g.Key)
.ToList();

关于Linq 按标签列表过滤帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39022615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com