相关疑难解决方法(0)

集合<T>与List <T>您应该在接口上使用什么?

代码如下所示:

namespace Test
{
    public interface IMyClass
    {
        List<IMyClass> GetList();
    }

    public class MyClass : IMyClass
    {
        public List<IMyClass> GetList()
        {
            return new List<IMyClass>();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行代码分析时,我得到以下建议.

警告3 CA1002:Microsoft.Design:更改'IMyClass.GetList()'中的'List'以使用Collection,ReadOnlyCollection或KeyedCollection

我应该如何解决这个问题以及这里有什么好的做法?

.net c# collections code-analysis

152
推荐指数
3
解决办法
8万
查看次数

使用包含EF 4.1 Code-First的Include和/或Select方法时的订单导航属性?

这是这里解释的问题的第二步:EF 4.1代码优先:如何加载相关数据(父子孙子)?.在@Slauma的指导下,我使用这种方法成功检索了数据:

var model = DbContext.SitePages
    .Where(p => p.ParentId == null && p.Level == 1)
    .OrderBy(p => p.Order) // ordering parent 
    .ToList();

foreach (var child in model) { // loading children
    DbContext.Entry(child)
        .Collection(t => t.Children)
        .Query()
        .OrderBy(t => t.Order) // ordering children
        .Load();

    foreach (var grand in child.Children) { // loading grandchildren
        DbContext.Entry(grand)
            .Collection(t => t.Children)
            .Query()
            .OrderBy(t => t.Order) // ordering grandchildren 
            .Load();
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然这种方法有效,但它会向数据库发送许多查询,而我正在寻找一种方法,只需一个查询即可完成所有操作.在@Slauma的指导下(在上面链接的答案中解释),我已经将查询更改为:

var model2 = DbContext.SitePages
    .Where(p => p.ParentId …
Run Code Online (Sandbox Code Playgroud)

entity-framework parent-child ef-code-first entity-framework-4.1

8
推荐指数
1
解决办法
8763
查看次数