小编rex*_*ghk的帖子

在LINQ中使用2 where子句的性能

在LINQ-to-Entities中,您可以通过执行以下操作来查询实体:

var students = SchoolContext.Students.Where(s => s.Name == "Foo" && s.Id == 1);
Run Code Online (Sandbox Code Playgroud)

我知道在幕后它将被翻译成类似于以下内容的SQL:

SELECT *
FROM Students
WHERE Name = 'Foo' AND Id = 1
Run Code Online (Sandbox Code Playgroud)

但是如果我写的话,是否存在差异(关于性能):

var students = SchoolContext.Students
                            .Where(s => s.Name == "Foo")
                            .Where(s => s.Id == 1);
Run Code Online (Sandbox Code Playgroud)

它会被翻译成相同的SQL查询吗?从我的理解.Where()将返回IEnumerable<T>所以第二个.Where()将过滤内存中的实体而不是转换IQueryable<T>为SQL,这是正确的吗?

c# sql linq entity-framework

18
推荐指数
2
解决办法
2096
查看次数

jQuery源代码中的returnTrue和returnFalse函数

我不禁注意到jQuery的源代码中有两个看似无用的函数(对于v1.9.1,它的第2702行和第2706行):

function returnTrue() {
    return true;
}

function returnFalse() {
    return false;
}
Run Code Online (Sandbox Code Playgroud)

在jQuery中经常调用它们.是否有一个原因,他们不会简单地替代函数调用用布尔truefalse

javascript jquery

11
推荐指数
2
解决办法
786
查看次数

在ASP.NET MVC中显式指定控制器返回的ActionResult类型是一种好习惯

我一直在使用ASP.NET MVC一段时间,似乎发现自己不断从我的控制器返回ActionResult以外的东西.我显然返回了ViewResults,还有JSonResults以及我们内部构建的一些自定义结果.

我想知道,如果,而不是声明我的控制器方法,如:

public ActionResult Index()
Run Code Online (Sandbox Code Playgroud)

我应该开始宣布它们为

public ViewResult Index()
Run Code Online (Sandbox Code Playgroud)

要么

public JsonResult Search()
Run Code Online (Sandbox Code Playgroud)

如果我总是知道我的控制器上的索引操作将始终返回ViewResult,或者我的控制器上的搜索操作将始终返回JsonResult?

编辑:只是为了澄清,我正在具体谈论我总是希望返回特定类型的ActionResult的情况.

asp.net-mvc

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

Xunit Assert中的异步lambda表达式.Throws

我有一些测试代码断言重复Users无法通过我创建UserRepository.

User.cs:

public class User
{
    public int Id { get; set; }

    public string AccountAlias { get; set; }

    public string DisplayName { get; set; }

    public string Email { get; set; }

    public bool IsActive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

UserRepository.cs:

public class UserRepository
{
    public virtual async Task<User> CreateAsync(User entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }

        if (await GetDuplicateAsync(entity) != null)
        {
            throw new InvalidOperationException("This user already exists");
        } …
Run Code Online (Sandbox Code Playgroud)

c# xunit.net async-await

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

奇怪的FSharpLint警告

我想重新实现这个List.distinct功能:

let inline distinct list =
    let folder curr = function
        | [] -> [curr]
        | l -> if List.contains curr l then l else curr :: l
    List.foldBack folder list []
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

我有一个皮棉警告说

List.foldBack f x [] 也许可以被重构成 x

然而,这对我来说没有多大意义,因为这导致我返回原始列表而不执行不同的逻辑.

这是FSharpLint的错误吗?

f#

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

可以组成DbContext而不是继承吗?

假设我正在为一所学校进行代码优先开发,我有一个SchoolDbContext.有关实体框架的大多数文档建议您派生DbContext:

public class SchoolDbContext : DbContext
{
    public IDbSet<Student> Students => Set<Student>();
}
Run Code Online (Sandbox Code Playgroud)

但我的观点是SchoolDbContext从来没有的特例DbContext,它是不是仅仅利用的DbContext所以在我看来,SchoolDbContext应该组成DbContext:

public class SchoolDbContext
{
    private readonly DbContext _dbContext;

    public SchoolDbContext(DbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public IDbSet<Student> Students => _dbContext.Set<Student>();
}
Run Code Online (Sandbox Code Playgroud)

在我的ASP.NET MVC应用程序的组合根目录中,我通过设置这样的依赖项来尝试这种组合方法(使用Simple Injector作为示例):

private static void RegisterDependencies(Container container)
{
     // Assume I have a connection string SchoolDbContext
     container.Register(() => new DbContext("SchoolDbContext"), Lifestyle.Scoped);
     container.Register<SchoolDbContext>(Lifestyle.Scoped);
}
Run Code Online (Sandbox Code Playgroud)

Web.config文件:

<connectionStrings> …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc entity-framework simple-injector

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

加载NinjectModule的顺序

Ninject.Web.Common在我的ASP.NET MVC项目中安装了NuGet包:

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper Bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        Bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        Bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        try
        {
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc ninject

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

对复选框和单选按钮进行分组的正确方法

我有一个单选按钮列表,我知道根据W3C 标准,所有<form>控件都应该有与之关联的标签。我是这样做的:

<label class="radio-inline">
    <input id="foo1" name="gender" value="M" type="radio"> Male
</label>
<label class="radio-inline">
    <input id="foo2" name="gender" value="F" type="radio"> Female
</label>
Run Code Online (Sandbox Code Playgroud)

但是,我还希望有一个“标签”,告诉用户这两个单选按钮用于该gender字段。我知道这<label>不是一个选项,因为一个<label>仅与一个表单控件相关联。我搜索了一下,发现使用<fieldset>and<legend>是要走的路:

<fieldset class="form-group">
    <legend class="legend-label">Gender</legend>
    <label class="radio-inline">
        <input id="foo1" name="gender" value="M" type="radio"> Male
    </label>
    <label class="radio-inline">
        <input id="foo2" name="gender" value="F" type="radio"> Female
    </label>
</fieldset>
Run Code Online (Sandbox Code Playgroud)

我使用 CSS 将legend.legend-label要显示的样式设置为标签:

<label class="radio-inline">
    <input id="foo1" name="gender" value="M" type="radio"> Male
</label>
<label class="radio-inline">
    <input id="foo2" name="gender" value="F" type="radio"> Female …
Run Code Online (Sandbox Code Playgroud)

html semantic-markup twitter-bootstrap-3

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

如何重用具有略微不同的ProcessStartInfo实例的Process实例?

我有以下代码robocopy以a 开头Process.我还需要做数据库查询来确定每次robocopy调用我需要复制的目录,所以我用来ProcessStartInfo控制传递的参数.

internal class Program
{
    private static void Main(string[] args)
    {
        using (var context = new MyDbContext())
        {
            IEnumerable<ProcessStartInfo> processInfos = GetProcessInfos(context, args[0]);
            foreach (ProcessStartInfo processInfo in processInfos)
            {
                // How can I reuse robocopy Process instances and
                // how can I dispose of them properly?
                Process.Start(processInfo);
            }
        }
    }

    private static IEnumerable<ProcessStartInfo> GetProcessInfos(MyDbContext context,
                                                                 string directory)
    {
        const string defaultRobocopyFormatString = "{0} {1} /mir /tee /fft /r:3 /w:10 /xd *Temp*"; …
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework robocopy

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

在 Simple Injector 中注册具有原始配置依赖项的装饰器

我有一个IAppSettingsLoader接口,可以抽象出用于加载我的app.config文件的文件 IO 。

public interface IAppSettingsLoader
{
    IEnumerable<KeyValuePair<string, string>> LoadAppSettings();
}
Run Code Online (Sandbox Code Playgroud)

我有一个加载实际文件的类:

public class FileAppSettignsLoader : IAppSettingsLoader
{
    public IEnumerable<KeyValuePair<string, string>> LoadAppSettings()
    {
        // Perform actual loading through ConfigurationManager.AppSettings
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个缓存装饰器,试图监视文件更改 app.config

public class CachedAppSettingsLoader : IAppSettingsLoader
{
    private readonly ObjectCache _cache;
    private readonly string _cacheKey;
    private readonly CacheItemPolicy _cacheItemPolicy;
    private readonly IAppSettingsLoader _innerAppSettingsLoader;

    public CachedAppSettingsLoader(ObjectCache cache,
                                   string cacheKey, 
                                   CacheItemPolicy cacheItemPolicy, 
                                   IAppSettingsLoader innerAppSettingsLoader)
    {
        _cacheKey = cacheKey;
        _cacheItemPolicy = cacheItemPolicy;
        _cache = cache;
        _innerAppSettingsLoader = …
Run Code Online (Sandbox Code Playgroud)

.net c# dependency-injection simple-injector

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