English 中文(简体)
many
原标题:many to one linq projection
  • 时间:2011-08-24 13:11:01
  •  标签:
  • c#
  • linq

我怎么能够用一个问题来写这句话?

var query = this.ObjectContext.SomeCollection.
                .Where(...)
                .Select(f => new {somenumber = f.somenumber});

MyType type = new MyType()
                 {
                    Sum = query.Sum(f => f.somenumber)
                 }
最佳回答

你使用匿名类型是完全没有必要的,因为你在预测中只拥有一个财产。 您可以简单地回答问题,并将问题列入“<代码>的标本。 MyType。 请注意,只要你不重新利用其他地方的预测(在这种情况下,你会将其带走,然后重新使用)。

var type = new MyType {
               Sum = this.ObjectContext
                         .SomeCollection
                         .Where(SomeCondition)
                         .Select(f => f.somenumber)
                         .Sum()
           };

此外,您可减少<代码>。 选择(f => f.some number).Sum( to Sum(f => f.some number)

问题回答
MyType type = new MyType {
                Sum =
                 this.ObjectContext.SomeCollection
                 .Where(...)
                 .Select(f => f.somenumber)
                 .Sum() };

甚至

MyType type = new MyType {
                Sum =
                 this.ObjectContext.SomeCollection
                 .Where(...)
                 .Sum(f => f.somenumber) };

hou





相关问题
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. ...

热门标签