Note that there are some explanatory texts on larger screens.

plurals
  1. POIenumerable Extension method for dictionary array calls
    primarykey
    data
    text
    <p>Im trying to get an enumberable collection from a dictionary held array. Or should I say, I'm trying to write an extension method for my dictionary objects which store arrays to return an IEnumerable item when the result is null.</p> <p>Im using dictionarys to store array datasets (there are speed reasons for this), which I extract at certain search points. the extracted data is used in Linq queries, joins etc but I have problems when a data set doesnt exist.</p> <p>Returning an empty (0 count) row set would fix my problem. what I have so far is this (simplified code ofcourse)</p> <pre><code> public class Supplier { public string ID {get;set} public string Name {get;set} } private sups[] = new Supplier[10]; Dictionary&lt;int,Supplier[]&gt; dic = new Dictionary&lt;int, Supplier[]&gt;(); dic.Add(1,sups[]); public static IEnumerable&lt;Supplier&gt; TryGetValue&lt;Tkey&gt;(this IDictionary&lt;Tkey, Supplier[]&gt; source, Tkey ItemKey) { Supplier[] foundList; IEnumerable&lt;Supplier&gt; retVal; if (source.TryGetValue(ItemKey, out foundList)) { retVal = foundList.AsEnumerable(); } else { retVal = new Supplier[0].AsEnumerable(); } return retVal; } </code></pre> <p>// later in the code there is some thing like:</p> <pre><code>dic.TryGetValue(1).Count() //or a linq join from a in anothertable join d in dic.TryGetValue(1) on a.ID equals d.ID </code></pre> <p>What im trying to acheive is a generic extension method like below:</p> <pre><code>public static IEnumerable&lt;T&gt; TryGetValue&lt;Tkey,TValue&gt;(this IDictionary&lt;Tkey, TValue&gt; source, Tkey ItemKey) { // same code... // returning retVal = new T[0].AsEnumerable(); } </code></pre> <p>I keep getting close but never exactly there.... I would like to keep the extension method parameters simple. Its the passing of T which keeps catching me out.</p> <p>If anybody can help then please send me your feed back.</p> <p>many thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload