ojek
ojek

Reputation: 10078

Linq doesn't want to cooperate - DbExpressionBinding error

I am trying to get something from my database, i need it to be like that:

var tribunalCase = context.TribunalCases.Where(c => c.Voters.Any(v => v.Voter.UserName == User.Identity.Name))
                    .Select(c => c)
                    .ToList();

But then, it crashes when i try to use .Any() or .All(). I get the following error:

DbExpressionBinding requires an input expression with a collection ResultType. Parameter name: input

This is my model:

public class Tribunal
    {
        public int Id { get; set; }
        public Account User { get; set; }
        public DateTime Expires { get; set; }
        public Thread Thread { get; set; }
        public int Points { get; set; }
        public String Comment { get; set; }
        public int VotersCount { get; set; }
        public List<Voters> Voters { get; set; }
    }

    public class Voters
    {
        public int Id { get; set; }
        public Account Voter { get; set; }
        public bool Vote { get; set; }
        public Tribunal Tribunal { get; set; }
    }

Which i configured like that:

modelBuilder.Entity<Tribunal>()
                .HasOptional(t => t.Voters)
                .WithRequired();

How can i fix this error?

Upvotes: 0

Views: 886

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52745

Configuration is incorrect: Voters is a collection, so you should call HasMany, not HasOptional.

Upvotes: 2

Related Questions