English 中文(简体)
DefaultIfEmpty in LINQ
原标题:DefaultIfEmpty in LINQ
  • 时间:2012-01-13 16:51:06
  •  标签:
  • .net
  • linq

有些人可以解释<代码>DefaultIfEmpty()可在LINQ上使用。 我准备了一些材料,但仍然需要看到使用这些材料。

最佳回答

它基本上收回了一笔单项内容的藏书,因为收集来源是空的。

var numbers = new int[] {1, 2, 3};
var aNumber = numbers.First();

页: 1

but

var numbers = new int[];
var aNumber = numbers.DefaultIfEmpty(12).Single();

页: 12 as the collection is empty

问题回答

差异是,在第一OrDefault返回物体时,DefaultIfEmpty交还了一批物体。 如果没有发现任何结果,DefaultIfEmpty仍然归还一个具有违约价值的单一项目,而IOrDefault回报 T本身。

如果你们总是需要收集结果,例如制造外部结合,那么你就会使用“失败”。 如果你总是需要一个物体(而不是一个收集),那么你就使用第一号物体(或只是一个项目),例如,如果你想在寻找像身份证或独一无二的电子邮件这样的物品时,就希望归还你找不到的空件。

<代码>DefaultIfEmpty(>)特别有用的应用是,避免在你能够用<代码>Append仅处理一个空洞清单的情况下进行多次列举。

例如,认为你有一套(可能是空的)数字,需要平均价值。 如果定点为空,则你认为<代码>0为客户代码的合理价值。

你可以尝试

var avg = numbers.Append(0).Average() ;

but that doesn t actually give the correct results for a non-empty list!

页: 1

var avg = numbers.Any() ? numbers.Average() : 0 ;

但是,这两次是你确定的,如果你提出推迟的询问,那可能是大问题。

var avg = numbers.DefaultIfEmpty(0).Average();

只是你想要做的事——除非名单空洞,否则你就会获得平均数。

It s also nicely composable so a good building block for these kinds of methods...

public static T FuncOr<T>(this IEnumerable<T> items, 
                          Func<IEnumerable<T>, T> func, 
                          T fallback) 
                             => func(items.DefaultIfEmpty(fallback));

      
public static T MaxOr<T>(this IEnumerable<T> items, T fallback) 
                           => items.FuncOr(i=>i.Max(), fallback);

public static T MinOr<T>(this IEnumerable<T> items, T fallback) 
                           => items.FuncOr(i=>i.Min(), fallback);

//etc





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

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

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签