Note that there are some explanatory texts on larger screens.

plurals
  1. POC# generics with linq search criteria
    primarykey
    data
    text
    <p>I'm trying to make tiny helper method for simplify routine operations But on the sample:</p> <pre><code>public static int getEntityId&lt;Type, Entity&gt;(String name) where Entity: class { Type type = _db.GetTable&lt;Entity&gt;().SingleOrDefault(t =&gt; t.name == name); return 0; } </code></pre> <p>i get error:</p> <pre><code>Error 1 'Entity' does not contain a definition for 'name' and no extension method 'name' accepting a first argument of type 'Entity' could be found (are you missing a using directive or an assembly reference?) c:\work\asutp_migration\asutp_migration\Program.cs 89 62 asutp_migration </code></pre> <p>Actually, this is expected error, but how to solve this? All tables/classes i will use with this method have "name" field/property.</p> <p>UPD 1: according to advices i did this:</p> <pre><code>public partial class unit : IHasName { } interface IHasName { string name { get; } int id { get; } } public static int getEntityId&lt;Type, Entity&gt;(String name) where Entity: class, IHasName { Type type = _db.GetTable&lt;Entity&gt;().SingleOrDefault(t =&gt; t.name == name); return (type == null) ? type.id : 0; } </code></pre> <p>and with code:</p> <pre><code>int i = getEntityId&lt;unit&gt;("м"); Console.WriteLine(i); </code></pre> <p>i get exception:</p> <pre><code>Unhandled Exception: System.NotSupportedException: The mapping of interface member IHasName.name is not supported. </code></pre> <p>UPD 2: <a href="http://connect.microsoft.com/VisualStudio/feedback/details/344903/linq-to-sql-mapping-interface-member-not-supported-exception" rel="nofollow noreferrer">http://connect.microsoft.com/VisualStudio/feedback/details/344903/linq-to-sql-mapping-interface-member-not-supported-exception</a> i can't believe. is it related to my issue?</p> <p>UPD 3: yeah, seems it's VS issue: <a href="http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/bc2fbbce-eb63-4735-9b2d-26b4ab8fe589" rel="nofollow noreferrer">http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/bc2fbbce-eb63-4735-9b2d-26b4ab8fe589</a></p> <p>UPD 4:</p> <pre><code>public static int getEntityId&lt;Type&gt;(String name) where Type: class, IHasName { Type type = _db.GetTable&lt;Type&gt;().SingleOrDefault(t =&gt; t.name.Equals(name)); return (type != null) ? type.id : 0; } </code></pre> <p>so this is a solution, thank you all :-)</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.
 

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