# Ability to check for existance of a feature by Key

**URL:** <https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215>\
**Category:** WinForms\
**Created:** [February 6, 2009, 7:22am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215 "2009-02-06T07:22:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nelson](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nelson](https://community.thinkgeo.com/u/Nelson)\
**Post date:** [February 6, 2009, 7:22am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/1 "2009-02-06T07:22:36Z")

</div>

I&nbsp;have&nbsp;added&nbsp;a&nbsp;custom&nbsp;feature&nbsp;to&nbsp;the&nbsp;EditOverlay&nbsp;with&nbsp;a&nbsp;specific&nbsp;Key.&nbsp;I&nbsp;would&nbsp;like&nbsp;to&nbsp;know&nbsp;the&nbsp;best&nbsp;way&nbsp;to&nbsp;test&nbsp;if&nbsp;this&nbsp;given&nbsp;feature&nbsp;exists&nbsp;before&nbsp;trying&nbsp;to&nbsp;recreate&nbsp;a&nbsp;feature&nbsp;with&nbsp;the&nbsp;same&nbsp;Key.

---

<div class="post-metadata">

**Author:** ![brendan](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@brendan](https://community.thinkgeo.com/u/brendan)\
**Post date:** [February 7, 2009, 6:06am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/2 "2009-02-07T06:06:03Z")

</div>

I&nbsp;haven't&nbsp;used&nbsp;the&nbsp;EditOverlay.&nbsp;Looking&nbsp;at&nbsp;the&nbsp;code&nbsp;though,&nbsp;would&nbsp;this&nbsp;work?

&nbsp;

` InMemoryFeatureLayer layer = (InMemoryFeatureLayer)MapControl.EditOverlay.EditLayer;  Feature searchedFeature = layer.InternalFeatures.FirstOrDefault(entity => entity.Id == "My Key");   if (searchedFeature != null)  {      ///...  } `

---

<div class="post-metadata">

**Author:** ![Ben](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ben/32/25720_2.png) [@Ben](https://community.thinkgeo.com/u/Ben)\
**Post date:** [February 9, 2009, 12:54am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/3 "2009-02-09T00:54:28Z")

</div>

Brendan,&nbsp;&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;tried&nbsp;your&nbsp;code&nbsp;and&nbsp;come&nbsp;up&nbsp;with&nbsp;some&nbsp;ideas&nbsp;that&nbsp;  
&nbsp;1,&nbsp;MapControl.EditOverlay.EditLayer&nbsp;is&nbsp;an&nbsp;InMemoryFeatureLayer&nbsp;already,&nbsp;so&nbsp;you&nbsp;don’t&nbsp;need&nbsp;to&nbsp;do&nbsp;the&nbsp;conversion.&nbsp;  
&nbsp;2,&nbsp;I&nbsp;couldn’t&nbsp;find&nbsp;the&nbsp;method&nbsp;FirstOrDefault,&nbsp;that’s&nbsp;one&nbsp;of&nbsp;your&nbsp;Extension&nbsp;Method,&nbsp;right?&nbsp;  
&nbsp;3,&nbsp;just&nbsp;want&nbsp;to&nbsp;provide&nbsp;a&nbsp;simple&nbsp;explanation&nbsp;about&nbsp;that&nbsp;Lambda&nbsp;Expression&nbsp;in&nbsp;case&nbsp;some&nbsp;guys&nbsp;don’t&nbsp;very&nbsp;sure&nbsp;about&nbsp;it.&nbsp;  
&nbsp;&nbsp;  
&nbsp;The&nbsp;argument&nbsp;of&nbsp;FirstOrDefault&nbsp;is&nbsp;a&nbsp;handler,&nbsp;and&nbsp;here&nbsp;we&nbsp;passed&nbsp;a&nbsp;concrete&nbsp;method&nbsp;Entity&nbsp;=\>entity.id&nbsp;==&nbsp;“My&nbsp;Key”&nbsp;that&nbsp;means&nbsp;for&nbsp;whatever&nbsp;entity&nbsp;you&nbsp;input,&nbsp;if&nbsp;its&nbsp;id&nbsp;equals&nbsp;to&nbsp;“My&nbsp;Key”,&nbsp;it&nbsp;returns&nbsp;true&nbsp;otherwise&nbsp;returns&nbsp;false.&nbsp;&nbsp;  
&nbsp;&nbsp;  
&nbsp;Also,&nbsp;here&nbsp;is&nbsp;a&nbsp;simpler&nbsp;solution&nbsp;which&nbsp;does&nbsp;not&nbsp;use&nbsp;the&nbsp;extension&nbsp;method.&nbsp;  
&nbsp;&nbsp;  
&nbsp;`            if (winformsMap1.EditOverlay.EditLayer.InternalFeatures.Contains("My Key"))            {                /// …            }`&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Ben&nbsp;

---

<div class="post-metadata">

**Author:** ![brendan](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@brendan](https://community.thinkgeo.com/u/brendan)\
**Post date:** [February 9, 2009, 7:38am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/4 "2009-02-09T07:38:43Z")

</div>

1.&nbsp;MapControl.EditOverlay.EditLayer&nbsp;is&nbsp;an&nbsp;InMemoryFeatureLayer&nbsp;already,&nbsp;so&nbsp;you&nbsp;don’t&nbsp;need&nbsp;to&nbsp;do&nbsp;the&nbsp;conversion.  
  
[Yes&nbsp;ur&nbsp;right,&nbsp;Initially&nbsp;I&nbsp;was&nbsp;trying&nbsp;to&nbsp;cast&nbsp;from&nbsp;another&nbsp;collection&nbsp;as&nbsp;i&nbsp;was&nbsp;finding&nbsp;my&nbsp;way&nbsp;through&nbsp;the&nbsp;code,&nbsp;and&nbsp;forgot&nbsp;the&nbsp;cast&nbsp;in&nbsp;there]  
  
  
  
2.&nbsp;I&nbsp;couldn’t&nbsp;find&nbsp;the&nbsp;method&nbsp;FirstOrDefault,&nbsp;that’s&nbsp;one&nbsp;of&nbsp;your&nbsp;Extension&nbsp;Method,&nbsp;right?  
  
[It's&nbsp;a&nbsp;standard&nbsp;Linq&nbsp;extension&nbsp;method&nbsp;found&nbsp;in&nbsp;the&nbsp;"System.Linq.Expressions"&nbsp;namespace,&nbsp;and&nbsp;will&nbsp;safely&nbsp;return&nbsp;a&nbsp;null&nbsp;if&nbsp;a&nbsp;result&nbsp;is&nbsp;not&nbsp;found...&nbsp;and&nbsp;this&nbsp;is&nbsp;what&nbsp;i&nbsp;was&nbsp;testing&nbsp;for&nbsp;when&nbsp;checking&nbsp;if&nbsp;the&nbsp;searched&nbsp;item&nbsp;was&nbsp;null:P]  
  
  
  
Also,&nbsp;here&nbsp;is&nbsp;a&nbsp;simpler&nbsp;solution&nbsp;which&nbsp;does&nbsp;not&nbsp;use&nbsp;the&nbsp;extension&nbsp;method.&nbsp;[Ah,&nbsp;I&nbsp;didn't&nbsp;see&nbsp;that,&nbsp;thanks:P&nbsp;I&nbsp;wonder&nbsp;if&nbsp;that's&nbsp;using&nbsp;a&nbsp;for&nbsp;/&nbsp;foreach&nbsp;/&nbsp;linq&nbsp;statement&nbsp;/&nbsp;action]  
  
&nbsp;

Regards,

Brendan

---

<div class="post-metadata">

**Author:** ![Ben](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ben/32/25720_2.png) [@Ben](https://community.thinkgeo.com/u/Ben)\
**Post date:** [February 9, 2009, 7:54pm UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/5 "2009-02-09T19:54:18Z")

</div>

Brendan,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks&nbsp;for&nbsp;letting&nbsp;me&nbsp;know.&nbsp;I&nbsp;had&nbsp;a&nbsp;test&nbsp;and&nbsp;I&nbsp;think&nbsp;it&nbsp;should&nbsp;be&nbsp;“System.Linq”&nbsp;namespace&nbsp;I&nbsp;need&nbsp;to&nbsp;import&nbsp;for&nbsp;the&nbsp;built&nbsp;in&nbsp;Extension&nbsp;Methods.&nbsp;Here&nbsp;is&nbsp;a&nbsp;small&nbsp;sample&nbsp;for&nbsp;that.&nbsp;  
&nbsp;&nbsp;  
&nbsp;`using System;using System.Linq;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            int[] numbers = { };            int first = numbers.FirstOrDefault();        }    }}`&nbsp;  
&nbsp;&nbsp;  
&nbsp;I’m&nbsp;not&nbsp;sure&nbsp;which&nbsp;action&nbsp;“Contains”&nbsp;is&nbsp;using,&nbsp;may&nbsp;be&nbsp;“for”,&nbsp;as&nbsp;“Contains”&nbsp;doesn’t&nbsp;exist&nbsp;in&nbsp;IEnumerable,&nbsp;if&nbsp;it&nbsp;is&nbsp;using&nbsp;foreach,&nbsp;maybe&nbsp;MicroSoft&nbsp;would&nbsp;also&nbsp;add&nbsp;it&nbsp;to&nbsp;IEnumerable.&nbsp;Just&nbsp;guess&nbsp;:)&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Ben&nbsp;

---

<div class="post-metadata">

**Author:** ![brendan](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@brendan](https://community.thinkgeo.com/u/brendan)\
**Post date:** [February 10, 2009, 2:03am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/6 "2009-02-10T02:03:52Z")

</div>

Actually,&nbsp;with&nbsp;the&nbsp;extension&nbsp;methods&nbsp;it&nbsp;does:&nbsp;[msdn.microsoft.com/en-us/library/bb352880.aspx](http://msdn.microsoft.com/en-us/library/bb352880.aspx).&nbsp;  
&nbsp;&nbsp;  
&nbsp;As&nbsp;far&nbsp;as&nbsp;I&nbsp;know,&nbsp;IEnumerable.Contains&nbsp;is&nbsp;only&nbsp;implemented&nbsp;for&nbsp;the&nbsp;IEnumerable&nbsp;interface&nbsp;and&nbsp;not&nbsp;the&nbsp;IList&nbsp;interface.&nbsp;  
&nbsp;&nbsp;  
&nbsp;The&nbsp;only&nbsp;reason&nbsp;I&nbsp;noted&nbsp;it&nbsp;was&nbsp;purely&nbsp;because&nbsp;the&nbsp;Contains&nbsp;method&nbsp;in&nbsp;question&nbsp;is&nbsp;a&nbsp;ThinkGeo&nbsp;custom&nbsp;method&nbsp;on&nbsp;the&nbsp;GeoCollection&nbsp;class&nbsp;(which&nbsp;references&nbsp;System.Reflection&nbsp;-&nbsp;makes&nbsp;me&nbsp;nervous:P).&nbsp;The&nbsp;generic&nbsp;Contains&nbsp;methods&nbsp;in&nbsp;the&nbsp;framework&nbsp;require&nbsp;a&nbsp;matching&nbsp;object&nbsp;to&nbsp;be&nbsp;passed&nbsp;in&nbsp;(and&nbsp;preferably&nbsp;an&nbsp;IEqualityComparer&nbsp;-&nbsp;which&nbsp;is&nbsp;quite&nbsp;useful&nbsp;when&nbsp;working&nbsp;with&nbsp;complex&nbsp;classes).&nbsp;  
&nbsp;&nbsp;  
&nbsp;Anyway,&nbsp;this&nbsp;is&nbsp;off&nbsp;topic&nbsp;and&nbsp;not&nbsp;too&nbsp;important.&nbsp;

---

<div class="post-metadata">

**Author:** ![Nelson](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nelson](https://community.thinkgeo.com/u/Nelson)\
**Post date:** [February 10, 2009, 6:33am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/7 "2009-02-10T06:33:50Z")

</div>

Thanks&nbsp;for&nbsp;everything,&nbsp;guys.&nbsp;I&nbsp;was&nbsp;able&nbsp;to&nbsp;work&nbsp;around&nbsp;my&nbsp;particular&nbsp;issue&nbsp;but&nbsp;the&nbsp;flexibility&nbsp;to&nbsp;test&nbsp;for&nbsp;a&nbsp;features&nbsp;extistance&nbsp;by&nbsp;Key&nbsp;on&nbsp;demand&nbsp;will&nbsp;come&nbsp;up&nbsp;again&nbsp;soon!

Thanks&nbsp;again.

---

<div class="post-metadata">

**Author:** ![David](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@David](https://community.thinkgeo.com/u/David)\
**Post date:** [February 10, 2009, 6:45am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/8 "2009-02-10T06:45:59Z")

</div>

Nelson,&nbsp;  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;I&nbsp;am&nbsp;not&nbsp;quite&nbsp;sure&nbsp;I&nbsp;follow&nbsp;you.&nbsp;&nbsp;When&nbsp;you&nbsp;add&nbsp;the&nbsp;feature&nbsp;to&nbsp;the&nbsp;EditLayer.InternalFeatures&nbsp;you&nbsp;can&nbsp;specify&nbsp;your&nbsp;key&nbsp;as&nbsp;it&nbsp;is&nbsp;a&nbsp;dictionary.&nbsp;&nbsp;You&nbsp;can&nbsp;also&nbsp;call&nbsp;the&nbsp;Contains&nbsp;or&nbsp;ContainsKey&nbsp;on&nbsp;the&nbsp;dictionary&nbsp;also.&nbsp;&nbsp;You&nbsp;can&nbsp;later&nbsp;just&nbsp;get&nbsp;it&nbsp;back&nbsp;using&nbsp;the&nbsp;key.&nbsp;&nbsp;I&nbsp;think&nbsp;I&nbsp;am&nbsp;missing&nbsp;something&nbsp;or&nbsp;not&nbsp;reading&nbsp;you&nbsp;reply&nbsp;right.&nbsp;&nbsp;Maybe&nbsp;I&nbsp;don’t&nbsp;know&nbsp;this&nbsp;API&nbsp;well&nbsp;enough.&nbsp;&nbsp;Can&nbsp;you&nbsp;elaborate&nbsp;a&nbsp;little?&nbsp;  
&nbsp;&nbsp;  
&nbsp;David

---

<div class="post-metadata">

**Author:** ![Nelson](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nelson](https://community.thinkgeo.com/u/Nelson)\
**Post date:** [February 10, 2009, 6:55am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/9 "2009-02-10T06:55:46Z")

</div>

I&nbsp;think&nbsp;at&nbsp;the&nbsp;time&nbsp;I&nbsp;overlooked&nbsp;or&nbsp;missused&nbsp;the&nbsp;Contains&nbsp;method.&nbsp;The&nbsp;scenario&nbsp;I&nbsp;was&nbsp;bumping&nbsp;into&nbsp;was&nbsp;related&nbsp;to&nbsp;an&nbsp;event&nbsp;firing&nbsp;off&nbsp;during&nbsp;my&nbsp;FormLoad&nbsp;and&nbsp;by&nbsp;fixing&nbsp;this&nbsp;I&nbsp;had&nbsp;no&nbsp;need&nbsp;to&nbsp;check&nbsp;by&nbsp;key&nbsp;since&nbsp;I&nbsp;could&nbsp;always&nbsp;ensure&nbsp;the&nbsp;feature&nbsp;was&nbsp;added&nbsp;to&nbsp;the&nbsp;map&nbsp;onload&nbsp;before&nbsp;the&nbsp;ExtentChanged&nbsp;event&nbsp;fired&nbsp;off.&nbsp;So,&nbsp;User&nbsp;Error!&nbsp;&nbsp;  
&nbsp;&nbsp;  
&nbsp;But&nbsp;yes,&nbsp;the&nbsp;Contains&nbsp;function&nbsp;looks&nbsp;like&nbsp;it&nbsp;would&nbsp;do&nbsp;exactly&nbsp;what&nbsp;I&nbsp;had&nbsp;asked.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks&nbsp;again&nbsp;to&nbsp;everyone.

---

<div class="post-metadata">

**Author:** ![David](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@David](https://community.thinkgeo.com/u/David)\
**Post date:** [February 10, 2009, 7:07am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/10 "2009-02-10T07:07:08Z")

</div>

Nelson,&nbsp;  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;Thanks&nbsp;for&nbsp;clearing&nbsp;that&nbsp;up.&nbsp;&nbsp;I&nbsp;thought&nbsp;I&nbsp;was&nbsp;in&nbsp;the&nbsp;twilight&nbsp;zone&nbsp;for&nbsp;a&nbsp;bit.&nbsp;&nbsp;I&nbsp;saw&nbsp;Ben&nbsp;&&nbsp;Brendan&nbsp;talking&nbsp;about&nbsp;&nbsp;everything&nbsp;from&nbsp;Linq&nbsp;to&nbsp;Extension&nbsp;Methods.&nbsp;:-)&nbsp;  
&nbsp;&nbsp;  
&nbsp;David

---

<div class="post-metadata">

**Author:** ![brendan](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@brendan](https://community.thinkgeo.com/u/brendan)\
**Post date:** [February 10, 2009, 7:09am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/11 "2009-02-10T07:09:49Z")

</div>

hence&nbsp;my&nbsp;comment&nbsp;"Anyway,&nbsp;this&nbsp;is&nbsp;off&nbsp;topic&nbsp;and&nbsp;not&nbsp;too&nbsp;important."&nbsp;:P

---

<div class="post-metadata">

**Author:** ![David](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@David](https://community.thinkgeo.com/u/David)\
**Post date:** [February 10, 2009, 7:19am UTC](https://community.thinkgeo.com/t/ability-to-check-for-existance-of-a-feature-by-key/215/12 "2009-02-10T07:19:32Z")

</div>

Brendan,&nbsp;  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;You&nbsp;caught&nbsp;me.&nbsp;&nbsp;I&nbsp;am&nbsp;a&nbsp;skimmer…&nbsp;&nbsp;If&nbsp;I&nbsp;actually&nbsp;read&nbsp;all&nbsp;these&nbsp;posts&nbsp;I&nbsp;might&nbsp;loose&nbsp;my&nbsp;mind!&nbsp;  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;On&nbsp;thing,&nbsp;while&nbsp;we&nbsp;are&nbsp;on&nbsp;the&nbsp;subject,&nbsp;is&nbsp;that&nbsp;the&nbsp;extension&nbsp;methods&nbsp;are&nbsp;really&nbsp;slick!&nbsp;&nbsp;There&nbsp;is&nbsp;a&nbsp;dark&nbsp;side&nbsp;though&nbsp;I&nbsp;found.&nbsp;&nbsp;If&nbsp;you&nbsp;use&nbsp;them&nbsp;allot&nbsp;on&nbsp;common&nbsp;objects&nbsp;people&nbsp;start&nbsp;to&nbsp;get&nbsp;confused&nbsp;when&nbsp;reading&nbsp;your&nbsp;code.&nbsp;&nbsp;They&nbsp;seem&nbsp;shocked&nbsp;that&nbsp;X&nbsp;object&nbsp;has&nbsp;Y&nbsp;method.&nbsp;&nbsp;They&nbsp;know&nbsp;the&nbsp;objects&nbsp;pretty&nbsp;well&nbsp;and&nbsp;it&nbsp;throws&nbsp;them&nbsp;for&nbsp;a&nbsp;loop.&nbsp;&nbsp;It&nbsp;can&nbsp;make&nbsp;your&nbsp;code&nbsp;really&nbsp;simple&nbsp;though&nbsp;to&nbsp;encapsulate&nbsp;stupid&nbsp;loops&nbsp;logic&nbsp;into&nbsp;one&nbsp;sweet&nbsp;line&nbsp;of&nbsp;readable&nbsp;power&nbsp;code.&nbsp;  
&nbsp;&nbsp;  
&nbsp;David
