这个LINQ查询有什么问题

时间:2013-07-30 12:48:17

标签: c# entity-framework linq-to-entities

我正在编写LINQ查询以将现有列表与当前查询匹配。但它显示我的错误(无法创建类型'Vibrant.Areas.Acquisition.Models.BibContentsModel'的常量值。仅支持原始类型(如Int32,String和Guid')这个背景。)。请帮我解决问题。

代码

        FinalRRidInt = FinalRRid.Select(int.Parse).ToList();

        List<BibContentsModel> InitData = RRData().ToList();
        var FinalModel = (from aa in db.RecommendResources
                          where FinalRRidInt.Contains(aa.Id)
                          select new
                          {
                              RRId = aa.Id,
                              CurrentTitle = aa.Title,
                              CurrentISBN = aa.ISBN,
                              CurrentAuthor = aa.Author,
                              NewBibContentsModel = (from rr in InitData
                                                     where rr.Title.Contains(aa.Title) || rr.ISBN.Contains(aa.ISBN)
                                                     select new BibContentsModel
                                                                            {
                                                                                RRId = aa.Id,
                                                                                BibId = rr.BibId,
                                                                                Title = rr.Title,
                                                                                ISBN = rr.ISBN,
                                                                                Author = rr.Author,

                                                                            }).GroupBy(asd => asd.BibId).Select(asd => asd.FirstOrDefault())
                          }).AsEnumerable().Select(x => new RRModel
                          {
                              RRId = x.RRId,
                              CurrentAuthor = x.CurrentAuthor,
                              CurrentISBN = x.CurrentISBN,
                              CurrentTitle = x.CurrentTitle,
                              NewBibContentsModel = x.NewBibContentsModel.ToList()
                          });

InitData List的SQL查询方法

  public List<BibContentsModel> RRData()
    {
        List<BibContentsModel> Initdata = db.ExecuteStoreQuery<BibContentsModel>("select distinct b.id as BibId, stuff((select ' ' + bcc.NormValue from BibContents as bcc where bcc.BibId = b.Id and bcc.TagNo = '245' FOR XML PATH('') ), 1, 1, '') as Title,(select top(1) Normvalue from bibcontents bcon where (bcon.tagno='020' or bcon.tagno='022') and bcon.sfld='a' and bcon.bibid=b.id) as ISBN,(select top(1) Normvalue from bibcontents bcon where bcon.tagno='100' and bcon.sfld='a' and bcon.bibid=b.id) as Author from bibs b left join bibcontents bc on b.id=bc.bibid").ToList();
        return Initdata;
    }

由于

2 个答案:

答案 0 :(得分:1)

问题很可能在于NewBibContentsModel = x.NewBibContentsModel.ToList(),因为LINQ查询不支持引用非标量变量。

Referencing Non-Scalar Variables Not Supported

答案 1 :(得分:0)

InitDataBibContentsModel s的内存列表。 EF只能处理LINQ查询中的原始值的内存列表(与FinalRRidInt一样)。

因此,您必须找到一种方法将获取InitData的查询合并到最终查询中。也许这可以通过改变

来完成
from rr in InitData

进入

from rr in RRData()

如果RRData()是同一IQueryable<BibContentsModel>个实例的db