English 中文(简体)
合并两个简单的相关次要问题
原标题:Combining two simple related linq queries
  • 时间:2011-02-25 02:07:51
  •  标签:
  • c#
  • linq

I have two queries and i m using the result of the first one in the second one like this

 var temp  = (ObjectTable.Where(o => o.Category == "Y"));
 var anonymousObjList = temp.Select(o => new {o, IsMax = (o.Value == temp.Max(x => x.Value))});

是否有办法将这些问题纳入其中?

EDIT: I cannot just chain them directly because I m using temp.Max() in the second query.

最佳回答

http://www.ry syntax, 采用let关键词。 它只是一次评价最高比率,因此,它就像三个单独的声明一样,只是一行。

var anonymousObjList = from o in ObjectTable
                       where o.Category == "Y"
                       let max = ObjectTable.Max(m => m.Value)
                       select new { o, IsMax = (o.Value == max) };

这是我有史以来唯一一次使用问答语的国家。 你们可以不这样做。

编辑:ReSharper建议

var anonymousObjList = ObjectTable.Where(o => o.Category == "Y")
    .Select(o => new {o, max = ObjectTable.Max(m => m.Value)})
    .Select(@t => new {@t.o, IsMax = (@t.o.Value == @t.max)});

然而,这并不理想。 第一项选择是推出<代码>max。 每一物品的不动产——马克斯功能将按每个项目进行评估。 如果你使用 que子,它只评估一次。

另外,you只能用问询表-x<>。 我不是狂热的 que,而是这样做是值得的,也是我使用的唯一情况。 ReSharper是错误的。

问题回答

Why? it would be clearer (and more efficient) to make it three:

var temp  = (ObjectTable.Where(o => o.Category == "Y"));
int max = temp.Max(x => x.Value);
var anonymousObjList = temp.Select(o => new {o, IsMax = (o.Value == max)});

可能是最直截了当的背后因素,就是用时间价值取代所有“典型”事件。 由于这一价值似乎无法改变,因此,重新计算应当有效(简而言之):

var anonymousObjList = ObjectTable.Where(o => o.Category == "Y")
    .Select(o => new {o, IsMax = (o.Value == ObjectTable.Where(o => o.Category == "Y").Max(x => x.Value))});

正如已经指出的,这一疑问实际上与原始问题毫无好处,因为询问使用不同的执行,可以加以建立。 我实际上建议分配问题,甚至更多:

var temp  = (ObjectTable.Where(o => o.Category == "Y")); 
var maxValue = temp.Max(x => x.Value);    
var anonymousObjList = temp.Select(o => new {o, IsMax = (o.Value == maxValue)});

This is better than the original because every time "Max" is called causes another iteration over the entire dataset. Since it is being called in the Select of the original, Max was being called n times. That makes the original O(n^2)!





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