我是一位MS Lightswitch應用程序的新手程序員,幫助我公司的其他人進入採購。其中一個要求是,用戶只能看到他們被允許查看預算項目的購買項目。我有一個用戶表,我可以從Application.User.Name屬性和另一個部門表中成功識別。兩者通過另一個名爲PermissionGlues的表以多對多關係連接。Entity_Filter方法的Lambda範圍-MS Lightswitch 2012
所有這些工作,我的問題是在_Filter方法本身。我目前的代碼如下。
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;
}
}
最後一行它設置過濾器不工作,和電燈開關屏幕顯示紅色的X而不是用鼠標懸停數據「無法加載數據。請檢查您的網絡連接,然後再試一次。」我知道這一行是問題,因爲調試器在執行該行之前顯示所有其他變量都是正確的,並且在註釋行中進行替換不會導致錯誤。
我已經重寫了部門.Equals()函數(下面的代碼)以確保.Contains()可以正常工作,但奇怪的是每個方法中的斷點都不會被打中。這讓我懷疑拉姆達沒有被執行,因爲我明白了。
這就是說,我誤解了lambda語句的語法?有沒有更好的方法來執行此檢查?任何意見或建議將不勝感激。
謝謝你, --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();
}
}