问题
LINQ to Entities does not recognize the method 'System.String ToBase64String(Byte[])' method, and this method cannot be translated into a store expression.
var activityList = (from item in committeeMemberList
let committee = db.Committee.FirstOrDefault(x => x.Committee_Id == item.Committee_Id)
let contact = db.Contacts.FirstOrDefault(x => x.Contact_Id == item.Contact_Id)
select new Activity
{
Id = Convert.ToBase64String(item.Committee_Member_SPE_Id),
Name = committee.Committee_Name,
...
...
}).ToList();
回答1:
Change your LINQ so that your original statement returns a list of anonymous objects, and then select on THAT list and use the ToBase64String function:
var activityList =
(from item in
(from member in committeeMemberList
let committee = db.Committee.FirstOrDefault(x => x.Committee_Id == item.Committee_Id)
let contact = db.Contacts.FirstOrDefault(x => x.Contact_Id == item.Contact_Id)
select new
{
Id = member.Committee_Member_SPE_Id,
Name = committee.Committee_Name,
...
...
}).ToList())
select new Activity
{
Id = Convert.ToBase64String(item.Id),
Name = committee.Committee_Name,
...
...
}).ToList();
回答2:
var activityList =
(from item in
(from member in committeeMemberList
let committee = db.Committee.FirstOrDefault(x => x.Committee_Id == item.Committee_Id)
let contact = db.Contacts.FirstOrDefault(x => x.Contact_Id == item.Contact_Id)
select new
{
Id = member.Committee_Member_SPE_Id,
Name = committee.Committee_Name,
...
...
}).ToList());
//After Collecting information just update current value to base4string using following Syntax
activityList.ForEach(s=>s.id=(s.id==null?"noimage":Convert.ToBase4String(s.id));
来源:https://stackoverflow.com/questions/15392425/linq-to-entities-does-not-recognize-the-method-system-string-tobase64stringbyt