Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to choose programaticaly the column to be queried by Linq using PropertyInfo?
    text
    copied!<p>I would like to control how Linq queries my database programmatically. For instance, I'd like to query the column <strong>X</strong>, column <strong>Y</strong>, or column <strong>Z</strong>, depending on some conditions.</p> <p>First of all, I've created an array of all the properties inside my class called <code>myPropertyInfo</code>.</p> <pre><code>Type MyType = (typeOf(MyClass)); PropertyInfo[] myPropertyInfo = myType.GetProperties( BindingFlags.Public|BindingFlags.Instance); </code></pre> <p>The <code>myPropertyInfo</code> array allows me to access each property details (Name, propertyType, etc) through the index <code>[i]</code>.</p> <p>Now, how can I use the above information to control how Linq queries my DB?</p> <p>Here's a sample of a query I'd like to exploit.</p> <pre><code>var myVar = from tp in db.MyClass select tp.{expression}; </code></pre> <p><strong>Expression</strong> using <code>myPropertyInfo[i]</code> to choose which property (column) to query. </p> <p>I'm not sure if that's the way of doing it, but if there's another way to do so, I'll be glad to learn.</p> <p><strong>EDIT</strong>:</p> <p>I believe the right expression the one used by @Gabe. In fact, I'd like to make queries on the fly. Here's the reason: I've (i) a table <code>Organizations</code> (Ministries, Embassies, International Organizations, such as UN, UNPD, UNICEF, World Bank, etc, and services depending on them). I've (ii) an other table <code>Hierarchy</code> which represents the way those organizations are linked, starting by which category each one belongs to (Government, Foreign Missions, private sector, NGO, etc.) </p> <p>Each column representing a level in the hierarchy, some rows will be longer while other will be shorter. Many rows' columns will share the same value (for instance 2 ministries belonging to the government, will have "Government" as value for the column 'Level 1').</p> <p>That's why, for each row (organization), I need to go level by level (i.e. column by column).</p> <p>Hope I've been enough explicit this time</p> <p>Thanks for helping.</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