Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Instead of using a switch statement, you can use a lookup dictionary. This is psuedocode-ish, but this is one way to get your table in question. You'll have to manually maintain the dictionary, but it should be much easier than a switch. </p> <p>It looks like the <a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.gettable.aspx" rel="nofollow noreferrer">DataContext.GetTable()</a> method could be the answer to your problem. You can get a table if you know the type of the linq entity that you want to operate upon.</p> <pre><code>Dictionary&lt;string, Type&gt; lookupDict = new Dictionary&lt;string, Type&gt; { "Colour", typeof(MatchingLinqEntity) ... } Type entityType = lookupDict[AttributeFromRouteValue]; YourDataContext db = new YourDataContext(); var entityTable = db.GetTable(entityType); var entity = entityTable.Single(x =&gt; x.Id == IdFromRouteValue); // or whatever operations you need db.SubmitChanges() </code></pre> <p>The <a href="http://code.google.com/p/sutekishop/" rel="nofollow noreferrer">Suteki Shop project</a> has some <em>very</em> slick work in it. You could look into their implementation of <code>IRepository&lt;T&gt;</code> and IRepositoryResolver for a generic repository pattern. This really works well with an IoC container, but you could create them manually with reflection if the performance is acceptable. I'd use this route if you have or can add an IoC container to the project. You need to make sure your IoC container supports open generics if you go this route, but I'm pretty sure all the major players do.</p>
 

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