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. Inserting Dummy Items into LINQ Queries

Inserting Dummy Items into LINQ Queries

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqtutorialquestion
10 Posts 4 Posters 24 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.
  • T Offline
    T Offline
    Tristan Rhodes
    wrote on last edited by
    #1

    Is there any way to perform a LINQ query that returns a dummy item at the top? For example, i'm populating a DropDownList with a supplied data source object that contains a set of values. But i would like the DisplayMember to be a place holder , and it's DataValue to be Null. I'd like to avoid dumping the whole lot to a list first, as then i can't use Anonymous Types. Maybe something like this: dlCategories.DataSource = (from component in ??? select new {"[Select]", 0 }).Union (from component in items select new { component.Category, component.CategoryId }); Cheers ------------------------------- Carrier Bags - 21st Century Tumbleweed.

    modified on Thursday, February 07, 2008 6:00:21 AM

    M I R 3 Replies Last reply
    0
    • T Tristan Rhodes

      Is there any way to perform a LINQ query that returns a dummy item at the top? For example, i'm populating a DropDownList with a supplied data source object that contains a set of values. But i would like the DisplayMember to be a place holder , and it's DataValue to be Null. I'd like to avoid dumping the whole lot to a list first, as then i can't use Anonymous Types. Maybe something like this: dlCategories.DataSource = (from component in ??? select new {"[Select]", 0 }).Union (from component in items select new { component.Category, component.CategoryId }); Cheers ------------------------------- Carrier Bags - 21st Century Tumbleweed.

      modified on Thursday, February 07, 2008 6:00:21 AM

      M Offline
      M Offline
      Mark Churchill
      wrote on last edited by
      #2

      Wrong location to add that. Your code will then read as if the "select" placeholder is a "real" component. Switch AppendDataBoundItems to true on dlCategories and add a ListItem for your select programatically before databinding. Then you can also only add the "Select" if you have a new record :)

      Mark Churchill Director Dunn & Churchill Free Download:
      Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

      T 2 Replies Last reply
      0
      • M Mark Churchill

        Wrong location to add that. Your code will then read as if the "select" placeholder is a "real" component. Switch AppendDataBoundItems to true on dlCategories and add a ListItem for your select programatically before databinding. Then you can also only add the "Select" if you have a new record :)

        Mark Churchill Director Dunn & Churchill Free Download:
        Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

        T Offline
        T Offline
        Tristan Rhodes
        wrote on last edited by
        #3

        You know... i didn't even know that existed. I've perpetrated some horrific hacks in my time, many of which could have been averted by that :D Damn... Cheers!

        ------------------------------- Carrier Bags - 21st Century Tumbleweed.

        1 Reply Last reply
        0
        • M Mark Churchill

          Wrong location to add that. Your code will then read as if the "select" placeholder is a "real" component. Switch AppendDataBoundItems to true on dlCategories and add a ListItem for your select programatically before databinding. Then you can also only add the "Select" if you have a new record :)

          Mark Churchill Director Dunn & Churchill Free Download:
          Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

          T Offline
          T Offline
          Tristan Rhodes
          wrote on last edited by
          #4

          Hi Mark, I still can't do this. The issue is the one referenced here: http://www.codeproject.com/KB/combobox/UnboundItemsComboBox.aspx[^] But i'm trying to bind directly to a LINQ data context to save on object creation, so i'm trying to avoid Lists or custom data sources. I'm going to have to look at other options. Cheers

          ------------------------------- Carrier Bags - 21st Century Tumbleweed.

          M 1 Reply Last reply
          0
          • T Tristan Rhodes

            Hi Mark, I still can't do this. The issue is the one referenced here: http://www.codeproject.com/KB/combobox/UnboundItemsComboBox.aspx[^] But i'm trying to bind directly to a LINQ data context to save on object creation, so i'm trying to avoid Lists or custom data sources. I'm going to have to look at other options. Cheers

            ------------------------------- Carrier Bags - 21st Century Tumbleweed.

            M Offline
            M Offline
            Mark Churchill
            wrote on last edited by
            #5

            Something that came to mind just then:

            IEnumerable< T> MarksCrappySelectAdder(IEnumerable< T> foo)
            {
            yield return dummy;
            while(foo.movenext) yield return foo.current;
            }

            Mark Churchill Director Dunn & Churchill Free Download:
            Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

            T 1 Reply Last reply
            0
            • M Mark Churchill

              Something that came to mind just then:

              IEnumerable< T> MarksCrappySelectAdder(IEnumerable< T> foo)
              {
              yield return dummy;
              while(foo.movenext) yield return foo.current;
              }

              Mark Churchill Director Dunn & Churchill Free Download:
              Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

              T Offline
              T Offline
              Tristan Rhodes
              wrote on last edited by
              #6

              Cool, that's exactly what i was looking for. Pretty shocking that we have to resort to fudging it tho :( Cheers

              ------------------------------- Carrier Bags - 21st Century Tumbleweed.

              1 Reply Last reply
              0
              • T Tristan Rhodes

                Is there any way to perform a LINQ query that returns a dummy item at the top? For example, i'm populating a DropDownList with a supplied data source object that contains a set of values. But i would like the DisplayMember to be a place holder , and it's DataValue to be Null. I'd like to avoid dumping the whole lot to a list first, as then i can't use Anonymous Types. Maybe something like this: dlCategories.DataSource = (from component in ??? select new {"[Select]", 0 }).Union (from component in items select new { component.Category, component.CategoryId }); Cheers ------------------------------- Carrier Bags - 21st Century Tumbleweed.

                modified on Thursday, February 07, 2008 6:00:21 AM

                I Offline
                I Offline
                Insincere Dave
                wrote on last edited by
                #7

                What about IEnumerable.Concat in the from clause:int[] nums1 = { 1 }; int[] nums2 = { 2, 3, 4, 5 }; var q = from n in nums1.Concat(nums2) select n;

                1 Reply Last reply
                0
                • T Tristan Rhodes

                  Is there any way to perform a LINQ query that returns a dummy item at the top? For example, i'm populating a DropDownList with a supplied data source object that contains a set of values. But i would like the DisplayMember to be a place holder , and it's DataValue to be Null. I'd like to avoid dumping the whole lot to a list first, as then i can't use Anonymous Types. Maybe something like this: dlCategories.DataSource = (from component in ??? select new {"[Select]", 0 }).Union (from component in items select new { component.Category, component.CategoryId }); Cheers ------------------------------- Carrier Bags - 21st Century Tumbleweed.

                  modified on Thursday, February 07, 2008 6:00:21 AM

                  R Offline
                  R Offline
                  Roger Alsing 0
                  wrote on last edited by
                  #8

                  >>I'd like to avoid dumping the whole lot to a list first, as then i can't use Anonymous Types. yes you can.. You can do it like this: var res = (from component in items select new { Display = component.Category, ID = component.CategoryId }).ToList (); res.Insert (0, new { Display = "[Select]", ID = 0 }); dlCategories.DataSource = res; //Roger

                  http://www.puzzleframework.com

                  R 1 Reply Last reply
                  0
                  • R Roger Alsing 0

                    >>I'd like to avoid dumping the whole lot to a list first, as then i can't use Anonymous Types. yes you can.. You can do it like this: var res = (from component in items select new { Display = component.Category, ID = component.CategoryId }).ToList (); res.Insert (0, new { Display = "[Select]", ID = 0 }); dlCategories.DataSource = res; //Roger

                    http://www.puzzleframework.com

                    R Offline
                    R Offline
                    Roger Alsing 0
                    wrote on last edited by
                    #9

                    I found the problem quite interesting, so I played a bit more with it.

                    dlCategories.DataSource =
                    Enumerable.Repeat(new { Display = "[Select]", ID = 0 }, 1) //yield a single item
                    .Union (
                    from item in Items
                    select new { Display = item.Category, ID = item.CategoryId})
                    .ToList();

                    That should do it :-)

                    http://www.puzzleframework.com

                    T 1 Reply Last reply
                    0
                    • R Roger Alsing 0

                      I found the problem quite interesting, so I played a bit more with it.

                      dlCategories.DataSource =
                      Enumerable.Repeat(new { Display = "[Select]", ID = 0 }, 1) //yield a single item
                      .Union (
                      from item in Items
                      select new { Display = item.Category, ID = item.CategoryId})
                      .ToList();

                      That should do it :-)

                      http://www.puzzleframework.com

                      T Offline
                      T Offline
                      Tristan Rhodes
                      wrote on last edited by
                      #10

                      Excellent. Exactly what i needed. You don't need to use the .ToList() tho :)

                      ------------------------------- Carrier Bags - 21st Century Tumbleweed.

                      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