小编use*_*116的帖子

将基类转换为派生类

我想知道为什么这会适当地填充派生类:

BaseClass bc = MethodThatReturnsBaseClassObject();
DerivedClass dc = bc as DerivedClass;
Run Code Online (Sandbox Code Playgroud)

但是这会创建一个null派生类对象:

DerivedClass dc = MethodThatReturnsBaseClassObject() as DerivedClass;
Run Code Online (Sandbox Code Playgroud)

c# asp.net inheritance type-conversion

6
推荐指数
1
解决办法
1万
查看次数

Linq Select许多包括父项

我有以下三个Linq To Sql实体:Location,Institution和Building。

位置表只是一个LocationId和LocationTypeId。

机构和建筑物都从位置表中以一对一关系获取其ID,而机构表与建筑物表具有一对多关系。

我正在尝试返回机构及其所有子代的所有位置记录。以下查询返回我所需要的内容,但它缺少父机构位置记录。

var query = dc.Locations.SelectMany(l => l.Institutions, (loc, inst) => new { loc, inst })
                        .SelectMany(i => i.inst.Buildings, (locInst, bld) => bld.Location );
Run Code Online (Sandbox Code Playgroud)

我怎样才能把父母和孩子们包括在内?

具有相关列的ERD

更新:

如果在原始位置查询上使用联合,则可以获得我想要的记录,但是我仍然想知道这是否是最佳解决方案。这是与联盟的代码。

IQueryable<Location> locations = dc.Locations;

locations.SelectMany(l => l.Institutions, (loc, inst) => new { loc, inst })
         .SelectMany(i => i.inst.Buildings, (locInst, bld) => bld.Location)
         .Union(locations);
Run Code Online (Sandbox Code Playgroud)

c# linq parent-child sql-server-2008 linq-to-sql

6
推荐指数
1
解决办法
2067
查看次数

无法构造String类型.您必须配置容器以提供此值

我已经看了几个关于如何为MVC 5项目实现统一的教程和stackoverflow问题,但我似乎无法通过这个错误:

无法构造String类型.您必须配置容器以提供此值.

我安装了NuGet包Unity.Mvc5,并在unity配置中注册了我的类型.我还在unityconfig文件中调用了register components方法.

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();
        container.RegisterType<IStateService, StateService>();

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
}

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    UnityConfig.RegisterComponents();
}

public class StateController : Controller
{
    private readonly IStateService stateService;

    public StateController(IStateService stateService)
    {
        this.stateService = stateService;
    }

    // GET: /State/
    public ActionResult Index() …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc entity-framework unity-container asp.net-mvc-5

4
推荐指数
2
解决办法
1万
查看次数