小编Ani*_*ish的帖子

选择与实体框架中的位置之间的区别

实体框架.Select().Where()实体框架之间有什么区别?例如

return ContextSet().Select(x=> x.FirstName == "John")
Run Code Online (Sandbox Code Playgroud)

VS

ContextSet().Where(x=> x.FirstName == "John")
Run Code Online (Sandbox Code Playgroud)

我何时应该使用.Selectvs .Where

.net c# linq entity-framework linq-to-sql

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

类似于Visual Studio单元测试中的NUnit TestCaseSource

在Visual Studio单元测试中是否有类似于NUnit TestCaseSource属性的东西?我发现最接近的解决方案是使用DataSource.但我不想将我的测试用例参数存储在数据源中.

testing unit-testing visual-studio

7
推荐指数
1
解决办法
1978
查看次数

为什么最终没有被执行?

我的假设是,只要程序正在运行,finally块就会被执行.但是,在此控制台应用程序中,finally块似乎没有被执行.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                throw new Exception();
            }
            finally
            {
                Console.WriteLine("finally");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

产量

结果

注意:当抛出异常时,Windows询问我是否要结束应用,我说'是'.

.net c# exception try-catch try-finally

6
推荐指数
2
解决办法
240
查看次数

为什么异步等待抛出NullReferenceException?

我的代码看起来像这样

var userStartTask = LroMdmApiService.AddUser(user);
 // .... do some stuff
await userStartTask;
Run Code Online (Sandbox Code Playgroud)

AddUser()抛出一个异常,它冒泡的NullReferenceException.它不等待等待.

但是,如果我像这样构造代码......

var result = await LroMdmApiService.AddUser(user);
Run Code Online (Sandbox Code Playgroud)

然后正常捕获异常.谁能告诉我这里发生了什么?

以下是显示问题的完整代码.这种情况的最佳做法是什么?

class Program
{
    private static void Main(string[] args)
    {
        CallAsync();
        Console.ReadKey();
    }

    public static async void CallAsync()
    {
        var task = CallExceptionAsync();
        ThrowException("Outside");
        await task;
    }

    public static Task CallExceptionAsync()
    {
        return Task.Run(() =>
        {
            ThrowException("Inside");
        });

    }

    public static void ThrowException(string msg)
    {
        throw new Exception(msg);
    }        
}
Run Code Online (Sandbox Code Playgroud)

.net c# asynchronous visual-studio async-await

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

将DLL加载到单独的AppDomain中

我正在编写一个插件架构.我的插件dll位于运行插件管理器的子目录中.我将插件加载到单独的AppDomain中,如下所示:

string subDir;//initialized to the path of the module's directory.
AppDomainSetup setup = new AppDomainSetup();
setup.PrivateBinPath = subDir;
setup.ApplicationBase = subDir;

AppDomain newDomain= AppDomain.CreateDomain(subDir, null, setup);

byte[] file = File.ReadAllBytes(dllPath);//dll path is a dll inside subDir
newDomain.Load(file);
Run Code Online (Sandbox Code Playgroud)

然而.newDomain.Load返回当前域尝试加载的程序集.因为插件dll位于子目录中,所以当前域不能也不应该看到这些dll,并且当前域抛出FileLoadException"ex = {"无法加载文件或程序集......或其依赖项之一.

问题是,我们可以将程序集加载到单独的AppDomain中而不返回已加载的程序集吗?

我知道我可以在当前域中为AssemblyResolve事件添加一个处理程序并返回null,但我宁愿不去这条路线.

提前致谢.

.net c# asp.net assemblies appdomain

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

模型中的Display属性是否违反视图和模型中的关注点分离

可能是一个愚蠢的问题.但是没有使用Display属性指定模型中的标题违反了关注点分离原则吗?标题不应该属于视图吗?

如果没有,有人可以解释为什么它属于模型吗?

.net c# model-view-controller asp.net-mvc asp.net-mvc-3

3
推荐指数
1
解决办法
237
查看次数

饼图中的值

如何使用ChartHelper在饼图中显示每个饼图的值?我正在使用MVC3/Razor语法.

试着这样做:

馅饼

该图片来自MVC中ChartHelper的本教程:

我的代码:

var bytes = new Chart(600, 300).AddSeries(
                    chartType: "pie",
                    legend: "Sales in Store per Payment Collected",
                    xValue: viewModel.SalesInStorePerPaymentCollected.XValues,
                    yValues: viewModel.SalesInStorePerPaymentCollected.YValues
                    )
                    .GetBytes("png");


            return File(bytes, "image/png");
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc mschart asp.net-mvc-3

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

"指定的强制转换无效"仅适用于MS版本的发布版本

当只从MSBuild 4.0进行发布构建时,我收到"指定的强制转换无效"有效.我在使用Visual Studio 2012的发布版本时对此进行了测试,但没有遇到此问题.我还使用MSBuild 4.0的调试版本对此进行了测试,但没有遇到此问题.

例外: 在此输入图像描述

    public abstract class CachedSessionBase : ISessionObject
{
    protected Dictionary<MethodBase, Object> _getAndSetCache = new Dictionary<MethodBase, object>();

    protected TResult SetAndGet<TResult>(ObjectFactory factory, Func<TResult> func)
    {
        StackTrace stackTrace = new StackTrace();
        var methodBase = stackTrace.GetFrame(1).GetMethod();

        if (!_getAndSetCache.ContainsKey(methodBase))
        {
            _getAndSetCache[methodBase] = func.Invoke();
        }

        return (TResult)_getAndSetCache[methodBase];
    }
Run Code Online (Sandbox Code Playgroud)

错误正在这条线上抛出

return (TResult)_getAndSetCache[methodBase];
Run Code Online (Sandbox Code Playgroud)

.net c# msbuild il visual-studio

3
推荐指数
2
解决办法
1457
查看次数

为什么 ILogger 不记录到 Azure 应用程序见解?

我正在直接从这里测试控制台应用程序的代码: https: //learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger#

我基本上复制了代码并将其指向一个新的 azure 应用程序见解实例。但是,应用程序见解中没有显示任何日志。我错过了什么吗?

 static void Main(string[] args)
        {
            // Create DI container.
            IServiceCollection services = new ServiceCollection();

            // Add the logging pipelines to use. We are using Application Insights only here.
            services.AddLogging(loggingBuilder =>
            {
                // Optional: Apply filters to configure LogLevel Trace or above is sent to ApplicationInsights for all
                // categories.
                loggingBuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
                loggingBuilder.AddApplicationInsights(******);
            });

            // Build ServiceProvider.
            IServiceProvider serviceProvider = services.BuildServiceProvider();

            ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();


            logger.LogCritical("critical message working");
            // Begin a new scope. This is …
Run Code Online (Sandbox Code Playgroud)

.net c# azure azure-application-insights .net-core

3
推荐指数
1
解决办法
8687
查看次数

打字稿和 Jquery 导入 - $ 不是函数

我在 jquery 中使用打字稿,但我不断收到

未捕获的类型错误:$ 不是函数

有没有人见过这个?

我正在将 typescript 编译为 ES2017,然后使用 webpack 编译为 ES5。

//tsconfig
{
      "compileOnSave": true,
      "compilerOptions": {
        "module": "es2015",

        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": true,
        "target": "es2017",
        "noImplicitAny": false,
        "outDir": "Output",
        "esModuleInterop": true

      }
    }
Run Code Online (Sandbox Code Playgroud)

如何使用 jquery

import * as $ from "jquery";
 var form = $(document.createElement('form'));
Run Code Online (Sandbox Code Playgroud)

浏览器看到jquery($) 在此处输入图片说明

但后来我明白了

在此处输入图片说明

javascript jquery npm typescript

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