Linq to Entities中String.Join之后的Where子句

时间:2013-12-03 06:18:25

标签: c# entity-framework

我有一个Linq to Entites查询,我已经实现了逻辑。但是,我在字符串连接后执行了查询where子句。顺便说一句,我需要知道如何在执行where过滤器之前先加入字符串。当我在字符串连接之后执行查询where子句时,过滤记录需要很长时间。由于我的数据库有近20万个样本记录。

以下是我的查询内容:

// But the below query throws string join cannot be converted to store expression.

var NewBibContentsModel = (from x in db.BibContents
                                       where (x.TagNo == "245" && string.Join(" ", x.NormValue) == aa.CurrentTitle) || ((x.TagNo == "020") || (x.TagNo == "022") && string.Join(" ", 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),
                 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),
                RRId = aa.RRId
            }).ToList();

在字符串加入后使用Where子句进行查询

//以下查询需要花费大量时间来过滤记录。

var NewBibContentsModel = (from x in db.BibContents

                                       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),
                RRId = aa.RRId
            }).Where (x=> x.Title == aa.CurrentTitle || (x.ISBN == aa.CurrentISBN)).ToList();

对此问题的任何帮助或建议都将不胜感激。

谢谢,

0 个答案:

没有答案