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. phpstrom 7.0 序列号

    phpstrom 7.0 用户名 www.goipc.cn license key 13748-1204201000001dZdtN0KxZbl8zNH8RHFCz9t"gjKv9yXf2c ...

  2. .net speed up

    Ngen.exe http://www.cnblogs.com/yukaizhao/archive/2011/11/07/how-to-use-ugen.html Merge.exe Merge dl ...

  3. error: format not a string literal and no format arguments [-Werror=format-security]

    You can put this in your Application.mk to disable treating those warnings as errors: APP_CFLAGS += ...

  4. NodeJS - Express 4.0下使用app.dynamicHelpers错误

    在NodeJS - Express 4.0下使用app.dynamicHelpers发生错误: app.dynamicHelpers({ ^ TypeError: Object function (r ...

  5. SQL SERVER 強制指定使用索引 -转载 只为学习

    今天很高兴 ,有学会了一种数据库优化的方式,哈哈 今天遇到一個查詢逾時的問題:兩段SQL,只差在WHERE,一個是WHERE COLUMN1='AAA',一個是WHERE COLUMN1='BBB', ...

  6. PythonChallenge 2:爬虫和正则表达式

    题目: 解题思路:题目里已经说的很清楚了,字符可能在网页的源代码里.右键查看网页源代码,发现其中有一段:find rare characters in the mess below.有些人是直接把下面 ...

  7. 数字PID控制算法

    增量式PID控制算法 量式PID控制算法 2009-07-18 10:33 (转载 出处blog.ednchina.com/tengjingshu )blog.ednchina.com/tengjin ...

  8. js函数:setInterval()/clearInterval()——js网页计时器

    一.setInterval()/clearInterval()技术学习 都是window对象的方法,可以直接使用. setInterval(function(){},1000);:每1000毫秒执行一 ...

  9. spoj 78

    数学  组合 隔板法 #include <iostream> #include <cstring> #include <cstdio> #include <s ...

  10. Spring @ Component 的作用

    1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spr ...