0

我有一个从 Linq 到实体查询生成的列表。其中,我需要基于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();

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

谢谢

4

1 回答 1

0

您要实现的目标是Distinct ByMoreLinq有一个功能。语法如下:

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

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

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

于 2013-10-12T19:57:24.293 回答