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. Filtering LINQ query into another 'var' variable [modified]

Filtering LINQ query into another 'var' variable [modified]

Scheduled Pinned Locked Moved LINQ
tutorialcsharpdatabaselinqhelp
5 Posts 4 Posters 0 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.
  • K Offline
    K Offline
    K V Sekhar
    wrote on last edited by
    #1

    Hi all, I just trying get limited no'of columns from an var(Linq) query output. For example,

    var query = from t in dc.Table_Name select new { t.id,t.name,t.description};

    The above query output which contains 3 columns in each row. I wants to get the first column say 'id' into another variable. like :

    var query_filter = from t in query select t.id;

    but its giving error : the result can't be enumerated morethan once. Please suggest me how to get that. Thanks in advance, cheers sekhar

    modified on Thursday, February 26, 2009 4:22 AM

    A J 2 Replies Last reply
    0
    • K K V Sekhar

      Hi all, I just trying get limited no'of columns from an var(Linq) query output. For example,

      var query = from t in dc.Table_Name select new { t.id,t.name,t.description};

      The above query output which contains 3 columns in each row. I wants to get the first column say 'id' into another variable. like :

      var query_filter = from t in query select t.id;

      but its giving error : the result can't be enumerated morethan once. Please suggest me how to get that. Thanks in advance, cheers sekhar

      modified on Thursday, February 26, 2009 4:22 AM

      A Offline
      A Offline
      ABitSmart
      wrote on last edited by
      #2

      You will need to use First/FirstOrDefault,

      var query_filter = (from t in query select t.id).First();

      or

      var query_filter = (from t in query select t.id).FirstOrDefault();

      J 1 Reply Last reply
      0
      • A ABitSmart

        You will need to use First/FirstOrDefault,

        var query_filter = (from t in query select t.id).First();

        or

        var query_filter = (from t in query select t.id).FirstOrDefault();

        J Offline
        J Offline
        Jon Rista
        wrote on last edited by
        #3

        You shouldn't have to use First or FirstOrDefault. When you create a linq query, the result you get out (assuming your not running an aggregate function or First/Last/FirstOrDefault/LastOrDefault) is not the actual results of the query...its ultimately and IEnumerable that defines what the query should return when it is enumerated. You should be able to apply multiple modifications of your query before you actually iterate it:

        var query = from t in dc.Table_Name select new { t.id, t.name, t.description };
        IEnumerable queryOfID = from t in query select t.ID;

        foreach (int id in queryOfID)
        {
        Console.WriteLine(id);
        }

        The above should work. The only scenario where it may not work is if you iterate over query once, then try to create queryOfID, and iterate over that. Even with LINQ to SQL, iterating over a query that returns data from a database more than once should work, however there may be certain scenarios where subsequent iterations may be prevented to prevent unexpected modification of a database (would depend...not sure of an example scenario right now that might cause that to happen).

        K 1 Reply Last reply
        0
        • J Jon Rista

          You shouldn't have to use First or FirstOrDefault. When you create a linq query, the result you get out (assuming your not running an aggregate function or First/Last/FirstOrDefault/LastOrDefault) is not the actual results of the query...its ultimately and IEnumerable that defines what the query should return when it is enumerated. You should be able to apply multiple modifications of your query before you actually iterate it:

          var query = from t in dc.Table_Name select new { t.id, t.name, t.description };
          IEnumerable queryOfID = from t in query select t.ID;

          foreach (int id in queryOfID)
          {
          Console.WriteLine(id);
          }

          The above should work. The only scenario where it may not work is if you iterate over query once, then try to create queryOfID, and iterate over that. Even with LINQ to SQL, iterating over a query that returns data from a database more than once should work, however there may be certain scenarios where subsequent iterations may be prevented to prevent unexpected modification of a database (would depend...not sure of an example scenario right now that might cause that to happen).

          K Offline
          K Offline
          K V Sekhar
          wrote on last edited by
          #4

          Thanks for your suggestion

          1 Reply Last reply
          0
          • K K V Sekhar

            Hi all, I just trying get limited no'of columns from an var(Linq) query output. For example,

            var query = from t in dc.Table_Name select new { t.id,t.name,t.description};

            The above query output which contains 3 columns in each row. I wants to get the first column say 'id' into another variable. like :

            var query_filter = from t in query select t.id;

            but its giving error : the result can't be enumerated morethan once. Please suggest me how to get that. Thanks in advance, cheers sekhar

            modified on Thursday, February 26, 2009 4:22 AM

            J Offline
            J Offline
            jaypatel512
            wrote on last edited by
            #5

            Why dont you use this query? var result = from t in query Select new("id"); Thats it,. I might have mistaken in synatx near new . . .But its just something like that. . And you will get only one column . .

            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