您可以使用“ 组加入”来执行此操作,并使用以下方法加入组中的所有类别ID String.Join
:-
var result = (from n in namelist join c in categories on n.Id equals c.NameId into g select new { id = n.Id, Name = n.Name, CategorieIds = g.Select(x => x.CategoryId) }).AsEnumerable() .Select(x => new { Id = x.id, Name = x.Name, CategoryIds = String.Join(",",x.CategorieIds)) });