質問

I am a novice programmer working on a MS Lightswitch application to help others in my company enter purchases. One of the requirements is that users can only see purchases that have been charged to a budget line they're allowed to see. I have a table of users, which I can successfully identify from the Application.User.Name property, and another table of departments. The two are connected in a many-to-many relationship through another table I've named PermissionGlues.

All of this works, my problem is in the _Filter method itself. My current code is below.

partial void TransactionLines_Filter(ref Expression<Func<TransactionLine, bool>> filter)
    {
        //This is going to be a list of departments this user is allowed to see.
        List<Department> AllowedDepartments = new List<Department>();

        IEnumerable<User> UserEntities = (from User u in this.Users where u.UserName == Application.User.Name select u);
        User UserEntity = UserEntities.FirstOrDefault();
        if (UserEntity == null)
        {
            //The system user does not have an account
            filter = TL => false;
            return;
        }

        foreach(PermissionsGlue thisGlue in UserEntity.PermissionsGlues)
        {
            AllowedDepartments.Add(thisGlue.Department);
        }

        if (!this.Application.User.HasPermission(Permissions.NoFilter))
        {
            filter = TL => AllowedDepartments.Contains(TL.CreditDepartment);
            //filter = TL => true;
        }
    }

The last line which sets the filter doesn't work, and the lightswitch screen displays a red X instead of the data with a mouseover "Unable to load data. please check your network connection and try again." I know that this line is the problem because the debugger shows that all other variables are correct before this line executes, and substituting in the commented line doesn't cause the error.

I have overridden the department .Equals() functions (code below) to make sure .Contains() would work properly, but curiously a breakpoint in each of those methods is never hit. This is leading me to wonder if the lambda is not getting executed as I understood it.

All this is to say, am I misunderstanding the syntax of the lambda statement? Is there a better way to perform this check? Any advice or suggestions would be greatly appreciated.

Thank you kindly, --Ethan

public partial class Department
{
    partial void DepartmentNumber_Validate(EntityValidationResultsBuilder results)
    {
        if (DepartmentNumber != null)
        {
            if (DepartmentNumber.Length != 2)
                results.AddPropertyError("Department must be 2 digits.");
        }
    }

    public bool Equals(Department that)
    {
        if (that == null)
            return false;

        return this.DepartmentNumber == that.DepartmentNumber;
    }

    public override bool Equals(object that)
    {
        if (that != null && that is Department)
        {
            Department otherDept = (Department)that;
            return this.DepartmentNumber == otherDept.DepartmentNumber;
        }
        else
        {
            return false;
        }

    }

    public override int GetHashCode()
    {
        return this.DepartmentNumber.GetHashCode();
    }
}
役に立ちましたか?

解決

I finally found the right search keywords to turn up the answer. I'm posting in case anyone else ever has this same question.

http://social.msdn.microsoft.com/Forums/en-US/lightswitch/thread/135e7686-428b-4f70-a712-8f56232fb550

It turns out all of this was achievable with one line of code:

filter = TL => TL.CreditDepartment.PermissionsGlues.Any(g => g.User.UserName == Application.User.Name);

I'm still not certain why the other way didn't work, but this does exactly what I want, so I'm pleased.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top