【问题标题】:Nested Lists Linq to return 1 definitive list [duplicate]嵌套列表 Linq 返回 1 个最终列表 [重复]
【发布时间】:2014-10-06 15:23:53
【问题描述】:

我们有一个包含嵌套列表的列表,即。

public class Question
{
  public string Question { get; set;}
  public List<Tag> Tags { get; set; }
}

public class Tag
{
  public string TagName { get; set;}
  public string TagDescription { get; set;}
}

然后我们有一个 GetQuestions() 返回问题列表的过程

public List<Tag> QuestionTags(int Type)
{
DAQuestions da = new DAQuestions();
var t = (from d in da.GetQuestions(Type)
               )
                select d.Tags).ToList();
}

我们正在努力实现它以返回 1 个明确的标签列表(无重复)。

我们现在返回的是一个

List<List<Tag>>

【问题讨论】:

标签: c# linq asp.net-mvc-4


【解决方案1】:

你需要使用这样的东西:

da.GetQuestions(Type).SelectMany(q => q.Tags).Distinct().ToList();

Distinct 依赖查询提供程序来过滤掉相同的标签,因此它取决于您使用的 OR/M。

【讨论】:

    猜你喜欢
    • 2019-06-24
    • 1970-01-01
    • 2020-07-07
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多