Reputation: 195
I was writing a LINQ query to match the existing list with the current query. But It show's me error something like this(Unable to create a constant value of type 'Vibrant.Areas.Acquisition.Models.BibContentsModel'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.). Please help me to find out the problem.
Code
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()
});
Sql Query method for InitData List
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;
}
Thanks
Upvotes: 1
Views: 145
Reputation: 109255
InitData
is an in-memory list of BibContentsModel
s. EF can only handle in-memory lists of primitive values in LINQ queries (as it does with FinalRRidInt
).
So you must find a way to incorporate the query that obtains InitData
into the final query. Maybe this can be done by changing
from rr in InitData
into
from rr in RRData()
if RRData()
is an IQueryable<BibContentsModel>
off the same db
instance.
Upvotes: 0
Reputation: 2910
The problem most probably lies with your NewBibContentsModel = x.NewBibContentsModel.ToList()
since LINQ queries do not support referencing of non-scalar variables.
Referencing Non-Scalar Variables Not Supported
Upvotes: 1