开发者

.net mvc3 entity deep compare on Where

My Code for a controller:

public ViewResult Index(int? ProjectID)
    {
        var user = HttpContext.User;
        User profile = db.Users.Where(d => d.Email == user.Identity.Name).Single();
        var contracts = db.Contracts.Include(c => c.Project);
        if (!user.IsInRole("Admin"))
        {
            contracts = contracts.Where(p => p.Project.Client == profile.Client );
        }

        if (ProjectID != null)
        {
            contracts = contracts.Where(u => u.ID == ProjectID);
        }

        return View(contracts.ToList());
    }

This is suppose to pull up all of the contracts whose parent project has the same client fk as the user currently signed in unless they are an admin. This isn't working.

I am getting the following error when the non-admin's look at the page:

Unable to create a constant value of type 'MembershipExt.Models.Client'. Only primitive type开发者_运维技巧s ('such as Int32, String, and Guid') are supported in this context.

Do I need to use a second include or something?


What is the data type for p.Project.Client? I'm guessing that it is a complex type (it has properties off of it). Perhaps what you want is something like this...

contracts = contracts.Where(p => p.Project.Client.ClientID == profile.Client.ClientID );

Obviously I don't know what the object looks like, but perhaps this helps.


contracts = contracts.Where(p => p.Project.Client == profile.Client );

i am guessing the problem is in the above line the right hand side of lambda needs to be of simple type that is int32, string etc but you are having a complex type i.e. p.Project.Client


This looks wrong

db.Users.Where(d => d.Email == user.Identity.Name)

looks like you are comparing email with name

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜