Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems combining a function or extension method with linq to match aliases
    primarykey
    data
    text
    <p>In a database I take data from several aliases are used for one ID (a catch all ID) and the same happens for the value this ID needs to be compared to. Linq doesn't allow me however to use an extension or function to select records by, or I am doing something wrong here.</p> <p>Let's say that null,"_","", and "Default" are posible values for the Catch All ID, where as any other value should be considered as a seperate value.</p> <p>I'm looking for a way to generate a linq select which matches aliasses in a clean and maintainable way.</p> <p>What seems to works is:</p> <pre><code>using (DataContext context = new DataContext()) { var q = (from c in context.BasketContents where (c.ID+" ").Replace("Default", "").Replace("_", "") == (Value+" ").Replace("Default", "").Replace("_", "") select c); } </code></pre> <p>alternatively this also works:</p> <pre><code>where ((c.ID==null |c.ID=="Default" | c.ID=="_" | c.ID=="") &amp; (Value==null |Value=="Default" | Value=="_" | Value=="")) | (c.ID==Value)) </code></pre> <p>which is even worse :) </p> <p>Since this comparison needs to be made in a rather big number of queries I would like to use an extension, or a function, so I tried combining the following:</p> <pre><code> private bool IsSameID(string a1, string a2) { return ((a1 == null | a1 == "Default" | a1 == "_" | a1 == "") &amp;&amp; (a2 == null | a2 == "Default" | a2 == "" | a2 == "_")) | (a1 == a2) ? true : false; } </code></pre> <p>with the query:</p> <pre><code>using (DataContext context = new DataContext()) { var q = (from c in context.BasketContents where IsSameID(c.ID,Value) select c); } </code></pre> <p>this throws the following exception: Boolean 'IsSameID(System.String, System.String)' has no supported translation to SQL</p> <p>I also wrote an extension method which would take care of the replaces:</p> <pre><code>public static string DeAlias(this string a) { return (a == null | a == "Default" | a == "_" | a == "") ? "" : a; } </code></pre> <p>so I could compare <code>ID.DeAlias() == Value.DeAlias()</code>, but this basically has the same problem.</p> <p>So what am I doing wrong here?</p>
    singulars
    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.
 

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