1

我正在编写一个 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列表的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;
    }

谢谢

4

2 回答 2

1

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

不支持引用非标量变量

于 2013-07-30T13:10:06.887 回答
0

InitDataBibContentsModels 的内存列表。EF 只能处理 LINQ 查询中的原始值的内存列表(与FinalRRidInt.

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

from rr in InitData

进入

from rr in RRData()

ifRRData()IQueryable<BibContentsModel>同一个db实例。

于 2013-07-30T15:03:27.647 回答