- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有(或想要)这样的代码:
IDictionary<string,int> dict = new Dictionary<string,int>();
// ... Add some stuff to the dictionary.
// Try to find an entry by value (if multiple, don't care which one).
var entry = dict.FirstOrDefault(e => e.Value == 1);
if ( entry != null ) {
// ^^^ above gives a compile error:
// Operator '!=' cannot be applied to operands of type 'System.Collections.Generic.KeyValuePair<string,int>' and '<null>'
}
我也试过像这样更改违规行:
if ( entry != default(KeyValuePair<string,int>) )
但这也给出了一个编译错误:
Operator '!=' cannot be applied to operands of type 'System.Collections.Generic.KeyValuePair<string,int>' and 'System.Collections.Generic.KeyValuePair<string,int>'
这里有什么?
最佳答案
Jon 的回答适用于 Dictionary<string, int>
,因为它在字典中不能有空键值。它不适用于 Dictionary<int, string>
,但是,因为它不代表空键值...“失败”模式将以键 0 结束。
两种选择:
写一个TryFirstOrDefault
方法,像这样:
public static bool TryFirstOrDefault<T>(this IEnumerable<T> source, out T value)
{
value = default(T);
using (var iterator = source.GetEnumerator())
{
if (iterator.MoveNext())
{
value = iterator.Current;
return true;
}
return false;
}
}
或者,转换到可空类型:
var entry = dict.Where(e => e.Value == 1)
.Select(e => (KeyValuePair<string,int>?) e)
.FirstOrDefault();
if (entry != null)
{
// Use entry.Value, which is the KeyValuePair<string,int>
}
关于c# - Dictionary.FirstOrDefault() 如何确定是否找到结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5424968/
我遇到这样的问题:无论内部表达式如何计算,FirstOrDefault 始终返回它遇到的第一个项目。 假设我有一系列公司。 Companies = {Company1, Company2, Compa
我正在尝试在 IEnumerable 中搜索特定记录(使用谓词)。如果该记录不存在,我想返回第一条记录。如果那也不存在,我只想要 null。 我正在使用 var category = categori
有什么区别 FirstOrDefault(someField => someField.Name.Equals(settings.Text)) 和 Where(someField => someFie
使用 Linq to Entities 有以下区别吗? db.EntityName.Where(a => a.Id == id).FirstOrDefault(); db.EntityName.Fir
myCollection.Where(...).FirstOrDefault() 和 myCollection.FirstOrDefault(...) 之间是否存在性能差异 用您正在使用的谓词填充点。
我知道这个问题是asked a lot by people甚至有人说 So, first(FirstOrDefault(predicate)) one is better in terms of pe
有什么区别吗 DataFeedManager.LoadAllDataFeeds().FirstOrDefault(d => d.ItemID == itemId); 和 DataFeedManage
我一直在编写我的 LINQ 查询时使用 Where 子句中的谓词,后跟 FirstOrDefault 子句。我开始在 FirstOrDefault 子句中看到带有谓词的示例。 这个比那个好吗? EF
这两个 Linq 查询有什么区别: var result = ResultLists().Where( c=> c.code == "abc").FirstOrDefault(); // vs. va
我有点好奇,当谈到 FirstOrDefault 时,哪种被认为是“最佳实践”。 我已经看过这个问题,它与我的问题相似,但还不够接近,无法回答我的问题。 其中哪一个是“更好的代码”?为什么? var
FirstOrDefault() and FirstOrDefault() 有什么区别?在 LINQ 中? 有人能给我一些简单的解释吗? TypedDataSet ds= codeComponent.
我想声明如下: var block = blocksById.FirstOrDefault(X => X.Value == tracResult.ID).Key 我的问题是如果我有 FirstOrDe
我在 C# 项目中使用 EntityFramework 6.1.3 和 SQL Server。我有两个查询,基本上应该执行相同的操作。 1. Exams.GroupBy(x=>x.SubjectID)
这个问题在这里已经有了答案: EF Where(x => x.ColumnVal == 1) vs FirstOrDefault(x => x.Column == 1) (3 个答案) 关闭 9 年
我一直在清理一些旧代码,并遇到了以下结构的一些查询: var attribute = DbSet.Where().Select(a => a.Attribute).SingleOrDefault();
希望做一些重构......使用 NHibernate 我目前有这个查询 public Widget FindByCode(string code) { return
这里遇到了一种边缘情况问题。我的任务是将所有数据从一个数据库提取到另一个数据库,其中目标数据库具有不同的架构。 我选择编写一个 WinForms 实用程序来在必要时使用 Entity Framewor
下面是我编写的代码,由于 FirstOrDefault,它只返回第一条记录。获取存储在变量 EmailAddresses 中的所有电子邮件地址的语法应该是什么,其中 FlagActive 字段为 Tr
有两个主键a和b一键编译两把 key 怎么样 var product = db.TMP_HELLO.FirstOrDefault(instrmnt => (instrmnt.a == good.a;
当使用 Enumerable.FirstorDefault() 时,我是否需要始终捕获当操作的集合为 null 时可能抛出的 ArumentNullException? 过去我总是这样做: Workf
我是一名优秀的程序员,十分优秀!