Reputation: 960
I have a Linq query which supposed to work with number of parameters dynamically. Based on the null condition, the Where clause string should include/exclude from Where Clause string. I use third party Dynamic Linq library to achieve this. Here is the below code,
Code
var boo = (from b in db.Bibs
from inf in db.InfoTypes.Where(info => info.Id == b.InfoTypeId).DefaultIfEmpty()
from it in db.Items.Where(itms => itms.BibId == b.Id).DefaultIfEmpty()
from ic in db.Collections.Where(coll => coll.Id == it.CollectionId).DefaultIfEmpty()
from il in db.ItemLocations.Where(iloc => iloc.Id == it.LocationId).DefaultIfEmpty()
from aacc in db.AdminAccounts.Where(aacc => aacc.Id == b.CreatedBy).DefaultIfEmpty()
from bc in db.BibContents.Where(bc => bc.BibId == b.Id).DefaultIfEmpty()
.Where("inf.Description='E-Working Papers'")
select new Book
{
BibId = b.Id,
Type = inf.Description,
NoOfCopies = db.Items.Where(itms => itms.BibId == b.Id).Count(),
createdby = db.AdminAccounts.Where(acc => acc.Id == b.CreatedBy).Select(aac => aac.Name).FirstOrDefault(),
ModifiedBy = db.AdminAccounts.Where(acc => acc.Id == b.LastModifiedBy).Select(aac => aac.Name).FirstOrDefault(),
createdon = b.CreatedOn,
lastmodifiedon = b.LastModifiedOn,
catalogdate = b.CatalogDate
}).GroupBy(g => new { g.BibId }).Select(s => s.FirstOrDefault());
When I run the above code, I am getting
LINQ to Entities does not recognize the method System.Linq.IQueryable[Vibrant_ClassLibrary.BibContent] Where[BibContent](System.Linq.IQueryable[Vibrant_ClassLibrary.BibContent], System.String, System.Object[]) method, and this method cannot be translated into a store expression.
Upvotes: 1
Views: 509
Reputation: 22631
This is a similar problem as described here.
You have to insert .AsEnumerable()
before your .Where(queryString)
call; otherwise Entity Framework tries to convert the queryString
itself, which it isn't capable of.
Upvotes: 1