我正在尝试开发一个ASP.NET MVC 4应用程序,玩家可以根据他们的进攻,防守和助攻技能进行评分.攻击,防御和辅助是引用相同查找表的Player表上的外键 - 评级.
我有以下父实体:
public class Rating
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Player> Players { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和子实体:
public class Player
{
public int Id { get; set; }
public string Name { get; set; }
public int OffenceRatingId { get; set; }
[ForeignKey("OffenceRatingId")]
public virtual Rating OffenceRating { get; set; }
public int DefenceRatingId { get; set; }
[ForeignKey("DefenceRatingId")]
public virtual Rating DefenceRating …Run Code Online (Sandbox Code Playgroud) 我有一个带有以下函数的ASP.NET Framework 4.5应用程序,用于检查用户是否是AD组的成员:
public static bool IsUserGroupMember(string userName, string groupName)
{
string domain = "ad.our.org";
string defaultOU = "OU=Our_Department,DC=ad,DC=our,DC=org";
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domain, defaultOU, ContextOptions.SimpleBind);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, userName);
GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName);
return oGroupPrincipal.Members.Contains(oUserPrincipal);
}
Run Code Online (Sandbox Code Playgroud)
但是,这仅在用户直接是该组的成员而不是嵌套在该组中的另一个组的成员时才有效.
希望获得帮助来修复此代码,以便通过组内的每个嵌套组递归地检查成员资格.我查看了StackOverflow中类似问题的答案,但无法弄清楚如何最好地修改我的函数以使其工作.
谢谢.
这类似于问题(在给定的子 LINQ (lambda 表达式) 的树层次结构中查找父项)。但是,我需要找到所有后代,而不是找到所有祖先。
我正在修改 Yacoub 的方法,但只设法将所有后代都放在一个分支中。
private IEnumerable<UserRole> FindAllChildrenRecursively(List<UserRole> allRoles, UserRole role)
{
var child = allRoles.FirstOrDefault(x => x.ParentId == role.Id);
if (child == null)
return Enumerable.Empty<UserRole>();
return new[] { child }.Concat(FindAllChildrenRecursively(allRoles, child));
}
Run Code Online (Sandbox Code Playgroud) 我有表 Table1、Table2、Table3 等。为了处理审核,我使用包含以下字段 Id、Username、ChangeTime、ChangeType、TableName 和 TableRecordId 的 Audit 表。
要获取 Table1 中与 Table1.Id = Audit.TableRecordId 上的审核连接的所有记录,其中用户名是“jdoe”,例如,我有以下内容:
var results = db.Table1s
.Join(db.Audits.Where(a => a.Username == "jdoe"),
t => t.Id,
a => a.RecordId,
(t, a) => new { Table1 = t, Audit = a });
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,我收到以下错误:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType1`2[SampleApp.Models.Table1,AuditLibraryContext.Models.Audit]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[AuditTrackerSample.Models.Table1]'.
Run Code Online (Sandbox Code Playgroud)
让它工作我最终得到了以下代码:
var results = db.Table1s
.Join(db.Audits.Where(a => a.Username == "jdoe"),
t => t.Id,
a => …Run Code Online (Sandbox Code Playgroud) 我需要用空格+相同的字符替换以下字符,只要它们是字符串的第一个字符:
"-"
"+"
"="
Run Code Online (Sandbox Code Playgroud)
例如.
"+hello" should become " +hello"
"-first-second" should become " -first-second"
Run Code Online (Sandbox Code Playgroud)