C#Generic.ForEach不起作用?或EF简单的方法来排除属性

Ale*_*mov 0 c# linq entity-framework

我需要清楚List中的一些属性

CategoryAccount是类

获取清单

List<CategoryAccount> ret = context.CategoryAccounts.ToList();
Run Code Online (Sandbox Code Playgroud)

用ForEach清除

//Clear Accounts poperty to null
//Accounts is List<Acccount>
ret.ForEach(x => x.Accounts = null);
//Clear Owner poperty to null
//Owner is class Owner 
ret.ForEach(x => x.Owner = null);

//In result
ret[0].Account != null
ret[0].Owner != null
Run Code Online (Sandbox Code Playgroud)

或者在context.CategoryAccounts中排除属性.

我不想使用Select(x => new { prop1 = x.prop1, prop2 = x.prop2? ///}- 必须包含模型中的太多属性.

Kas*_*ols 5

你似乎在使用延迟加载.在为导航属性分配任何值之前,必须先触发加载.你可以用它来做Include.

List<CategoryAccount> ret = context.CategoryAccounts
    .Include(x => x.Accounts)
    .Include(x => x.Owner)
    .ToList();
//Clear with ForEach

//Clear Accounts poperty to null
//Accounts is List<Acccount>
ret.ForEach(x => x.Accounts = null);
//Clear Owner poperty to null
//Owner is class Owner 
ret.ForEach(x => x.Owner = null);
Run Code Online (Sandbox Code Playgroud)