http://stackoverflow.com/questions/793897/check-if-keyvaluepair-exists-with-linqs-firstordefault

问题:

I have a dictionary of type

Dictionary<Guid,int>

I want to return the first instance where a condition is met using

var available = m_AvailableDict.FirstOrDefault(p => p.Value == 0)

However, how do I check if I'm actually getting back a KeyValuePair? I can't seem to use != or == to check against default(KeyValuePair) without a compiler error. There is a similar thread here that doesn't quite seem to have a solution. I'm actually able to solve my particular problem by getting the key and checking the default of Guid, but I'm curious if there's a good way of doing this with the keyvaluepair. Thanks

If you just care about existence, you could use ContainsValue(0) or Any(p => p.Value == 0)instead? Searching by value is unusual for a Dictionary<,>; if you were searching by key, you could use TryGetValue.

One other approach:

       var record = data.Where(p => p.Value == 1)
.Select(p => new { Key = p.Key, Value = p.Value })
.FirstOrDefault();

This returns a class - so will be null if not found.

Check if KeyValuePair exists with LINQ's FirstOrDefault的更多相关文章

  1. loadrunner录制时弹出invalid application path!please check if application exists对话框

    问题:oadrunner录制时弹出invalid application path!please check if application exists对话框 原因:IE浏览器地址不对,需要手动重新选 ...

  2. [Javascript] Check both prop exists and value is valid

    Sometime you need to check one prop exists on the object and value should not be ´null´ or ´undefine ...

  3. check process id exists

    kill -0 pid sending the signal 0 to a given PID just checks if any process with the given PID is run ...

  4. 101个LINQ示例,包含几乎全部操作

    Restriction Operators Where - Simple public void Linq1() { , , , , , , , , , }; var lowNums = from n ...

  5. 【2020-02-11】1346. Check If N and Its Double Exist

    更多LeetCode解题详解 Easy Given an array arr of integers, check if there exists two integers N and M such ...

  6. 《果壳中的C# C# 5.0 权威指南》 - 学习笔记

    <果壳中的C# C# 5.0 权威指南> ========== ========== ==========[作者] (美) Joseph Albahari (美) Ben Albahari ...

  7. 使用roslyn代替MSBuild完成解决方案编译

    原本我是使用批处理调用 MSBuild 完成解决方案编译的,新版的 MSBuild 在 Visual Studio 2015 会自带安装. 当然在Visual Studio 2015 中,MSBuil ...

  8. petapoco-SQLServer模型增加注释

    petapoco是个基于T4模板的轻量级ORM,好用效率高,具体介绍略了 获取注释基本原理是调用数据库::fn_listextendedproperty函数,获取扩展属性MS_Description ...

  9. T4 代码生成 Demo (抽奖程序)

    参考自这位大狮的:  https://github.com/Pencroff/Dapper-DAL/blob/master/Dapper-DAL/Models/ModelGenerator.tt 项目 ...

随机推荐

  1. Win7下配置nginx和php5

    本文链接:http://www.cnblogs.com/cnscoo/archive/2012/09/03/2668577.html 一.准备工作: OS:Windows7 SP1 Nginx: ng ...

  2. ubuntu: 环境搭建

    1.修改更新源    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak    sudo gedit /etc/apt/sources.li ...

  3. Android之EditText

    EditText 属性介绍: maxLength:设置最大输入字符数. hint:设置空白提示文字. textColorHint:设置空白提示文字的颜色. enabled:设置是否可编辑(可以获得焦点 ...

  4. 中国IT人,你们是否从没想过开发一款伟大的产品?

    我也是今年刚毕业的,一毕业就做了猎头,从开始实习到正式工作,迄今为止接触的IT技术人不下上千人了.这里面有腾讯.阿里巴巴.百度.360.金山.金蝶.用友.华为.惠普等从事自主研发的大牛,也有很多软通. ...

  5. android 解决启动页面加载图片空白以及去掉标题栏

    有时候启动页面根据白天晚上来启动时实现加载不同的图片效果,但是加载时会出现短暂的空白,解决方法如下: android:theme="@android:style/Theme.Transluc ...

  6. C# 连接 Oracle 的几种方式[转]

    本文转自:http://www.cnblogs.com/storys/archive/2013/03/06/2945914.html 一:通过System.Data.OracleClient(需要安装 ...

  7. ios Camera学习笔记

    检测设备的摄像头是否可用: - (BOOL) isCameraAvailable{ return [UIImagePickerController isSourceTypeAvailable: UII ...

  8. 漫谈CGI FastCGI WSGI

    作者:auxten链接:https://zhuanlan.zhihu.com/p/20054757来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. CGI(Common ...

  9. 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作

    最近我做的一些项目,经常需要用到对应用程序的配置文件操作,如app.config和web.config的配置文件,特别是对配置文件中的[appSettings]和[connectionStrings] ...

  10. hdu 1242 Rescue(BFS,优先队列,基础)

    题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...