Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Get Column Dynamically in LINQ

Get Column Dynamically in LINQ

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqtutorial
2 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gandalf_TheWhite
    wrote on last edited by
    #1

    If anyone is having idea regarding my below query please let me know. - I’ve one table with 3 column Price_GBP, Price_EUR, Price_USD. - I want to execute one LINQ query which can get column dynamically - For example, in one function, if I pass “Price_GBP” than same query should return value of price from Price_GBP column same for Euro & US Dollar.

    Believe Yourself™

    D 1 Reply Last reply
    0
    • G Gandalf_TheWhite

      If anyone is having idea regarding my below query please let me know. - I’ve one table with 3 column Price_GBP, Price_EUR, Price_USD. - I want to execute one LINQ query which can get column dynamically - For example, in one function, if I pass “Price_GBP” than same query should return value of price from Price_GBP column same for Euro & US Dollar.

      Believe Yourself™

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      Well here's a possible solution using reflection:

      //a dummy class
      public class Price
      {
          public decimal Eur { get; set; }
          public decimal USD { get; set; }
      }
      
      //a dummy list
      List<Price> prices = new List<Price>();
      
          private void button1\_Click(object sender, EventArgs e)
          {
              prices.Add(new Price { Eur = 3.2m, USD = 4.1m });
              //option1 => use a separate method
              var result = (from rec in prices
                           select GetValue(rec, "Eur")).First();
              //option2 => inline it
              var result2 = (from rec in prices
                             select rec.GetType().GetProperty("USD").GetValue(rec, null)).First();
      
              MessageBox.Show(result.ToString());
              MessageBox.Show(Convert.ToDecimal(result2).ToString());
          }
      
          private decimal GetValue(Price pr, string propName)
          {
              return (decimal)pr.GetType().GetProperty(propName).GetValue(pr, null);
          }
      }
      

      Of course the "EUR" or "USD" will be a method param or something, not hard coded as in this example. Hope It helps.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups