Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

c# - Error: Only primitive types or enumeration types are supported in this context EF

This piece of code keep making this error. :

Unable to create a constant value of type 'Repository.DBModel.Subscriber'. Only primitive types or enumeration types are supported in this context.

I've Changed it a few times but it keeps coming up with this 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 greatly appreciate if someone could point out my error or come up with a more efficient way to do this.

In advance thanks for your time and help.


I know there are a few post with this error out there but when reading them I can't figure out how they relate to my problem. so I'm sorry if this is a common mistake and others have provided a solution

The object Subscribers is a List<Subscriber>

I don't seem to be able to find the line but. the stack trace does contain this.

at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at Repository.SubScribRepository.AddOrUpdateSubscribers(List1 Subscribers)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You use a local collection, Subscribers, directly in a LINQ statement. But these objects can't be translated into SQL. There are only mappings from primitive types to database types.

I'd suggest you use

var emails = Subscribers.Select(s => s.Email).ToList();

And proceed by 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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...