Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to Entities-- Havine a Guid in the where clause throws error

Tags:

c#

linq

Here is my code

var bms = from b in context.bookmark
                     join q in context.question1Set on b.bookmark_id equals q.question_id
                     join u in context.userinfo on b.bookmark_ownerid equals u.user_userid
                     where b.bookmark_ownerid == new Guid(userid.ToString()) && q.question_isdeleted == false //i think it dosent't like the new Guid
                     select new
                     {
                         u.user_username,
                         q.question_title
                     };

        foreach (var bm in bms)
        {
            question q = new question();
            q.Username = bm.user_username;
            q.Title = bm.user_username;
            ql.Add(q);
        }

The error I get is: Only parameterless constructors and initializers are supported in LINQ to Entities

I have no idea how to fix this. Any ideas?

like image 364
user161433 Avatar asked Dec 22 '22 08:12

user161433


1 Answers

Construct the Guid before you do the query:

Guid userGuid = new Guid(userid.ToString()); // What type is userid anyway?

var bms = 
     from b in context.bookmark
     join q in context.question1Set on b.bookmark_id equals q.question_id
     join u in context.userinfo on b.bookmark_ownerid equals u.user_userid
     where b.bookmark_ownerid == userGuid && !q.question_isdeleted
     select new
     {
         u.user_username,
         q.question_title
     };
like image 181
Jon Skeet Avatar answered Jan 04 '23 23:01

Jon Skeet