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
442 views
in Technique[技术] by (71.8m points)

c# - Unable to create a constant value of type 'System.Object' in Entity Framework

I have a table named UserTenders having many-to-one relationship with aspnet_Membership table.
I am using EntityFramework 4.0 and when I try something like this, it errors.

var tenders = ctx.UserTenders
    .Where(tender => tender.HasAdminApproved.Equals(true))
    .ToList();

The error is

System.NotSupportedException
Unable to create a constant value of type 'System.Object'.
Only primitive types ('such as Int32, String, and Guid') are supported in this context.

This snippet below works.

var tenders = ctx.UserTenders.ToList();

What could be wrong in my code? Feel like I am missing something very trivial.

I would like to filter all those rows that have the bit field HasAdminApproved as true

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Try replacing

.Where(tender => tender.HasAdminApproved.Equals(true))

With:

.Where(tender => tender.HasAdminApproved == true)

Or as previously suggested by @Ladislav Mrnka if your field is bool?

.Where(tender => tender.HasAdminApproved)

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

...