以下两个发言是否相同?
Users user = db.Users.First(u => (u.Username == username));
以及
var user = from u in db.Users
where u.Username == username
select u;
Users aUser = user.First();
以下两个发言是否相同?
Users user = db.Users.First(u => (u.Username == username));
以及
var user = from u in db.Users
where u.Username == username
select u;
Users aUser = user.First();
是的,这两个问题在功能上都是相同的。 汇编者实际上将进行第二次查询,将其转化为第一个查询,因为准则语言关键词只是C#的语文延伸。
与此类似的例子表明:
using System;
using System.Linq;
class Example
{
static void Main()
{
var names = new[] { "Andrew", "Nicole", "Michael",
"Joe", "Sammy", "Joshua" };
var shortNames = from n in names
where n.Length == 6
select n;
var first = names.First();
}
}
使用反射镜 我看到了实际编纂的法典:
internal class Example
{
private static void Main()
{
string[] names = new string[] { "Andrew", "Nicole", "Michael", "Joe", "Sammy", "Joshua" };
IEnumerable<string> shortNames = names.Where<string>(delegate (string n) {
return n.Length == 6;
});
string first = names.First<string>();
}
}
如你所知,编辑们对分类的调查表进行了改动,将推广方法从<代码>上调。 计算单位:。 准则查询关键词只是开发商的一种方便,即汇编者转换成方法电话。
<><>Edit>: 我确实注意到你两次问话之间的一个差别,我首先注意到了这两个问题。 您的第一份询问将稍快执行,因为它通过<代码>First方法。 自您第二次问询使用<代码>以来,这项工作将加快。 在方法首先过滤结果,然后打碎第一个记录。 第二点使用两种方法电话,而第一类只使用一种。
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...