【问题标题】:How to get distinct value from the list如何从列表中获得不同的价值
【发布时间】:2013-10-13 03:57:24
【问题描述】:

我有一个从 Linq to Entities 查询生成的列表。其中,我需要获取基于BibId 的唯一记录。我已尝试更改查询,但没有帮助获取基于 BibId 的唯一记录。

查询

aa.NewBibContentsModel = (from x in db.BibContents

                                      where (x.TagNo == "245" && x.NormValue == aa.CurrentTitle) || (x.TagNo == "020" && x.NormValue == aa.CurrentISBN) || (x.TagNo == "022" && x.NormValue == aa.CurrentISBN)
                                      select new
            {
                BibId = x.BibId,
                Title = (from a in db.BibContents where a.BibId == x.BibId && a.TagNo == "245" orderby a.Id ascending select a.NormValue),
                //Tit = (from a in db.BibContents where a.BibId == line.BibId && a.TagNo == "245" && a.Sfld == "a" select a.NormValue).FirstOrDefault(),
                Author = (from a in db.BibContents where a.BibId == x.BibId && splitted.Contains(a.TagNo) && a.NormValue != null select a.TagNo).FirstOrDefault(),
                ISBN = (from a in db.BibContents where a.BibId == x.BibId && a.NormValue != null && (a.TagNo == "020" || a.TagNo == "022") orderby a.Id ascending select a.NormValue)
            }).AsEnumerable().Select(x => new BibContentsModel
            {
                BibId = x.BibId,
                Title = string.Join(" ", x.Title),
                Author = string.Join(" ", (from a in db.BibContents where a.BibId == x.BibId && a.TagNo == x.Author orderby a.Id select a.NormValue)),
                ISBN = string.Join(" ", x.ISBN)
            }).ToList();

对此问题的任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: entity-framework-4 linq-to-entities


    【解决方案1】:

    你想要达到的目标是Distinct ByMoreLinq 有一个功能。语法如下:

    (from x in db.BibContentsNo == "022")
    ... // your query
    }).AsEnumerable()
    .DistinctBy(x => x.BibId) // <= MoreLinq
    

    所做的是将记录按BibId 分组并取每个组的第一个元素。

    您可以将 MoreLinq 作为 NuGet 包下载。

    【讨论】:

      猜你喜欢
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 2015-04-29
      • 1970-01-01
      • 2018-04-09
      • 2021-07-28
      相关资源
      最近更新 更多