Check if KeyValuePair exists with LINQ's FirstOrDefault
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的更多相关文章
- loadrunner录制时弹出invalid application path!please check if application exists对话框
问题:oadrunner录制时弹出invalid application path!please check if application exists对话框 原因:IE浏览器地址不对,需要手动重新选 ...
- [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 ...
- 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 ...
- 101个LINQ示例,包含几乎全部操作
Restriction Operators Where - Simple public void Linq1() { , , , , , , , , , }; var lowNums = from n ...
- 【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 ...
- 《果壳中的C# C# 5.0 权威指南》 - 学习笔记
<果壳中的C# C# 5.0 权威指南> ========== ========== ==========[作者] (美) Joseph Albahari (美) Ben Albahari ...
- 使用roslyn代替MSBuild完成解决方案编译
原本我是使用批处理调用 MSBuild 完成解决方案编译的,新版的 MSBuild 在 Visual Studio 2015 会自带安装. 当然在Visual Studio 2015 中,MSBuil ...
- petapoco-SQLServer模型增加注释
petapoco是个基于T4模板的轻量级ORM,好用效率高,具体介绍略了 获取注释基本原理是调用数据库::fn_listextendedproperty函数,获取扩展属性MS_Description ...
- T4 代码生成 Demo (抽奖程序)
参考自这位大狮的: https://github.com/Pencroff/Dapper-DAL/blob/master/Dapper-DAL/Models/ModelGenerator.tt 项目 ...
随机推荐
- ubuntu 14.04 安装 Quartus II 13.1 过程
神奇的linux! 第一步去官网注册然后下载对应的linux版本,包括软件和设备文件两部分,软件也就是quartus II nios ide,modelsim-altera这些,设备就是具体alter ...
- Financial Management
Financial Management 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 Larry graduated this year and finally ...
- Ubuntu下的网络配置(USTC)
1. 配置静态ip ubuntu的网络配置信息放在 /etc/network/interfaces 中 sudo gedit /etc/network/interfacesauto lo 下 ...
- Opencv图像与矩阵的操作
#include "stdafx.h" #include <cv.h> #include <cxcore.h> #include <highgui.h ...
- DIV+CSS高手必知的15个CSS常识
1.不要使用过小的图片做背景平铺.这就是为何很多人都不用 1px 的原因,这才知晓.宽高 1px 的图片平铺出一个宽高 200px 的区域,需要 200*200=40, 000 次,占用资源. 2.无 ...
- texCUBE() to CubemapSampler.Sample()
update dx9 to dx11 refers to CUBEMAP sampler texCUBE(CubeMpaSampler,normal) maybe change to Cubema ...
- windows下创建.htaccess文件之讲解
如果想在Windows操作系统下新增一个.htaccess 文件实现对页面的rewrite,任你如何右点鼠标或者选文件->新增去新增都不会成功的,Windows都会要求给个文件名称.如果你想通过 ...
- spoj 78
数学 组合 隔板法 #include <iostream> #include <cstring> #include <cstdio> #include <s ...
- Chp10: Scalability and Memory Limits
The Step-by-Step Approach break down a tricky problem and to solve problems using what you do know. ...
- 【hadoop2.6.0】用C++ 编写mapreduce
hadoop通过hadoop streaming 来实现用非Java语言写的mapreduce代码. 对于一个一点Java都不会的我来说,这真是个天大的好消息. 官网上hadoop streaming ...