Inserting Dummy Items into LINQ Queries
-
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
-
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
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. -
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.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.
-
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.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.
-
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.
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. -
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.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.
-
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
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;
-
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'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
-
>>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
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 :-)
-
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 :-)
Excellent. Exactly what i needed. You don't need to use the .ToList() tho :)
------------------------------- Carrier Bags - 21st Century Tumbleweed.