English 中文(简体)
L2E 这两条是相同的吗?
原标题:L2E these two are the same thing?

以下两个发言是否相同?

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方法。 自您第二次问询使用<代码>以来,这项工作将加快。 在方法首先过滤结果,然后打碎第一个记录。 第二点使用两种方法电话,而第一类只使用一种。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

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. ...

NSArray s, Primitive types and Boxing Oh My!

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 ...

C# Marshal / Pinvoke CBitmap?

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 ...

How to Use Ghostscript DLL to convert PDF to PDF/A

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, ...

Linqy no matchy

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. ...

热门标签