c# - Error: Only primitive types or enumeration types are supported in this context EF -
this piece of code keep making error. :
unable create constant value of type 'repository.dbmodel.subscriber'. primitive types or enumeration types supported in context.
i've changed few times keeps coming error.
using (subscriberdbhandler db = new subscriberdbhandler()) { ienumerable <subscriber> newsubscribers = subscribers .where(sub => db.subscriber .any(asub => !asub.email.equals(sub.email))); list<subscriber> updatesubscribers = db.subscriber .where(dbsub => subscribers .any(lsub => lsub.email .equals(dbsub.email))).tolist(); if(newsubscribers.count() >= 1) { db.subscriber.addrange(newsubscribers); } updatesubscribers.foreach(asub => asub.state = subscribers .firstordefault(sub => sub.email .equals(asub.email)).state ?? "error" ); db.savechanges(); }
i'd appreciate if point out error or come more efficient way this.
in advance time , help.
i know there few post error out there when reading them can't figure out how relate problem. i'm sorry if common mistake , others have provided solution
the object subscribers list<subscriber>
i don't seem able find line but. stack trace contain this.
at system.linq.enumerable.tolist[tsource](ienumerable
1 source) @ repository.subscribrepository.addorupdatesubscribers(list
1 subscribers)
you use local collection, subscribers
, directly in linq statement. these objects can't translated sql. there mappings primitive types database types.
i'd suggest use
var emails = subscribers.select(s => s.email).tolist();
and proceed using these strings (i.e. primitive values) in contains
statements like:
var newsubscribers = db.subscriber .where(dbsub => !emails.contains(dbsub.email)) .tolist(); var updatesubscribers = db.subscriber .where(dbsub => emails.contains(dbsub.email)) .tolist();
Comments
Post a Comment