我正在尝试获取记录的不同标签列表。除了标签名称总是第一个重复之外,它有点工作。
public EntityCollection<TagEntity> Tags
{
get
{
EntityCollection<TagEntity> x = new EntityCollection<TagEntity>()
{
new TagEntity(){Id=1, IsActive=true, Name="Tag1"},
new TagEntity(){Id=2, IsActive=true, Name="Tag2"},
new TagEntity(){Id=3, IsActive=true, Name="Tag3"},
new TagEntity(){Id=4, IsActive=true, Name="Tag1"},
new TagEntity(){Id=5, IsActive=true, Name="Tag5"},
new TagEntity(){Id=6, IsActive=true, Name="Tag6"},
};
return x;
}
}
public EntityCollection<StoryTagsEntity> StoryTags
{
get
{
EntityCollection<StoryTagsEntity> x = new EntityCollection<StoryTagsEntity>()
{
new StoryTagsEntity(){ TagId=1, StoryId=1},
new StoryTagsEntity(){ TagId=1, StoryId=2},
new StoryTagsEntity(){ TagId=2, StoryId=1},
new StoryTagsEntity(){ TagId=3, StoryId=1},
new StoryTagsEntity(){ TagId=2, StoryId=3},
new StoryTagsEntity(){ TagId=4, StoryId=1},
};
return x;
}
}
public List<string> FetchTagNamesByStory(bool? isActive, int StoryId)
{
var d = (from t in this.Tags
join st in this.StoryTags on t.Id equals st.StoryId
where t.IsActive == isActive.GetValueOrDefault() && st.StoryId == StoryId
select t.Name);
List<string> x = new List<string>();
foreach (var item in d)
{
x.Add(item);
}
return x;
}
这会导致像这样的标签列表:
Tag1,Tag1,Tag1,Tag1
应该是:
标记1,标记2,标记3,标记1
有什么想法吗?
答案 0 :(得分:0)
您将t.Id
加入st.StoryId
而不是st.TagId
。如果你传入StoryId
的1,你将永远得到Tag1。
答案 1 :(得分:0)
你是对的,你的加入错了,你也可以做到以下几点:
public List<string> FetchTagNamesByStory(bool? isActive, int storyId)
{
return (from t in Tags
join st in StoryTags on t.Id equals st.TagId
where t.IsActive == isActive.GetValueOrDefault() && st.StoryId == storyId
select t.Name).ToList();
}