Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>does it create and open new connection every time? </p> </blockquote> <p>No. Connections are cached.</p> <blockquote> <p>Should I cache the results somewhere</p> </blockquote> <p>No. Do not cache entire tables.</p> <blockquote> <p>should I make TicDatabaseEntities variable static and initialize it at class level? </p> </blockquote> <p>No. Do not retain a DataContext instance longer than a UnitOfWork.</p> <blockquote> <p>Should my class be static if it contains only static methods?</p> </blockquote> <p>Sure... doing so will prevent anyone from creating useless instances of the class.</p> <blockquote> <p>Also I've noticed that if I return result.First() instead of FirstOrDefault() and the query does not find a match, it will issue an exception</p> </blockquote> <p>That is the behavior of First. As such - I typically restrict use of First to IGroupings or to collections previously checked with .Any().</p> <hr> <p>I'd rewrite your existing method as:</p> <pre><code>using (TicDatabaseEntities db = new TicDatabaseEntities()) { bool result = db.Articles .Any(a =&gt; a.supplierArticleID.Equals(supplierIdent)); return result; } </code></pre> <p>If you are calling the method in a loop, I'd rewrite to:</p> <pre><code>private static Dictionary&lt;string, bool&gt; TicArticleExists (List&lt;string&gt; supplierIdents) { using (TicDatabaseEntities db = new TicDatabaseEntities()) { HashSet&lt;string&gt; queryResult = new HashSet(db.Articles .Where(a =&gt; supplierIdents.Contains(a.supplierArticleID)) .Select(a =&gt; a.supplierArticleID)); Dictionary&lt;string, bool&gt; result = supplierIdents .ToDictionary(s =&gt; s, s =&gt; queryResult.Contains(s)); return result; } } </code></pre>
    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.
    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