在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,这是正确的吗?
我不禁注意到jQuery的源代码中有两个看似无用的函数(对于v1.9.1,它的第2702行和第2706行):
function returnTrue() {
return true;
}
function returnFalse() {
return false;
}
Run Code Online (Sandbox Code Playgroud)
在jQuery中经常调用它们.是否有一个原因,他们不会简单地替代函数调用用布尔true或false?
我一直在使用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的情况.
我有一些测试代码断言重复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) 我想重新实现这个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的错误吗?
假设我正在为一所学校进行代码优先开发,我有一个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) 我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) 我有一个单选按钮列表,我知道根据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) 我有以下代码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) 我有一个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) c# ×6
asp.net-mvc ×3
.net ×2
asp.net ×2
async-await ×1
f# ×1
html ×1
javascript ×1
jquery ×1
linq ×1
ninject ×1
robocopy ×1
sql ×1
xunit.net ×1