Need flexibility when binding IEnumerable to ComboBox; still need DataTable?

R

Ronald S. Cook

Using LINQ, I can bind my IEnumerable to my ComboBox no problem.

However, in the past it would be a DataTable that I could insert a new row
(e.g. "(Please select...)") and then sort it with a DataView and ultimately
bind the DataView to the ComboBox.

So, how can I do this in LINQ?

Do I need to somehow convert the IEnumerable to a DataTable first and do it
like I used to? If so, how?

Or, can I manipulate IEnumerable more than I think I can? I'm pretty new to
it so don't know really what I can do with it.

Thanks for any help,
Ron
 
N

Nicholas Paldino [.NET/C# MVP]

Ron,

Well, you could just union the results of one enumerable with another
which produces just one element. This would give one enumerable that you
can bind to for the complete list. See the Union extension method on the
Enumerable class for more details.

If you really want it in a DataTble, then there is no easy way to
convert what is returned from IEnumerable to a DataTable, unless it is an
IEnumerable<DataRow>. In that case, you can call the methods on the
DataTableExtensions class to get a DataTable/DataView instance.
 
R

Ronald S. Cook

Thanks Nicholas.. very helpful! You wouldn't happen to have an example of
the union, would you?



Nicholas Paldino said:
Ron,

Well, you could just union the results of one enumerable with another
which produces just one element. This would give one enumerable that you
can bind to for the complete list. See the Union extension method on the
Enumerable class for more details.

If you really want it in a DataTble, then there is no easy way to
convert what is returned from IEnumerable to a DataTable, unless it is an
IEnumerable<DataRow>. In that case, you can call the methods on the
DataTableExtensions class to get a DataTable/DataView instance.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Ronald S. Cook said:
Using LINQ, I can bind my IEnumerable to my ComboBox no problem.

However, in the past it would be a DataTable that I could insert a new
row (e.g. "(Please select...)") and then sort it with a DataView and
ultimately bind the DataView to the ComboBox.

So, how can I do this in LINQ?

Do I need to somehow convert the IEnumerable to a DataTable first and do
it like I used to? If so, how?

Or, can I manipulate IEnumerable more than I think I can? I'm pretty new
to it so don't know really what I can do with it.

Thanks for any help,
Ron
 
N

Nicholas Paldino [.NET/C# MVP]

Ronald,

Let's say you have a list of states from some source.:

IEnumerable<string> states = ;

And you want to add one more:

states = states.Union(new string[] {"All States"});

Then states will have the fifty states, and an entry at the end "All
States".

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
Thanks Nicholas.. very helpful! You wouldn't happen to have an example of
the union, would you?



Nicholas Paldino said:
Ron,

Well, you could just union the results of one enumerable with another
which produces just one element. This would give one enumerable that you
can bind to for the complete list. See the Union extension method on the
Enumerable class for more details.

If you really want it in a DataTble, then there is no easy way to
convert what is returned from IEnumerable to a DataTable, unless it is an
IEnumerable<DataRow>. In that case, you can call the methods on the
DataTableExtensions class to get a DataTable/DataView instance.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Ronald S. Cook said:
Using LINQ, I can bind my IEnumerable to my ComboBox no problem.

However, in the past it would be a DataTable that I could insert a new
row (e.g. "(Please select...)") and then sort it with a DataView and
ultimately bind the DataView to the ComboBox.

So, how can I do this in LINQ?

Do I need to somehow convert the IEnumerable to a DataTable first and do
it like I used to? If so, how?

Or, can I manipulate IEnumerable more than I think I can? I'm pretty
new to it so don't know really what I can do with it.

Thanks for any help,
Ron
 
R

Ronald S. Cook

Thanks!

Nicholas Paldino said:
Ronald,

Let's say you have a list of states from some source.:

IEnumerable<string> states = ;

And you want to add one more:

states = states.Union(new string[] {"All States"});

Then states will have the fifty states, and an entry at the end "All
States".

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
Thanks Nicholas.. very helpful! You wouldn't happen to have an example
of the union, would you?



Nicholas Paldino said:
Ron,

Well, you could just union the results of one enumerable with another
which produces just one element. This would give one enumerable that
you can bind to for the complete list. See the Union extension method
on the Enumerable class for more details.

If you really want it in a DataTble, then there is no easy way to
convert what is returned from IEnumerable to a DataTable, unless it is
an IEnumerable<DataRow>. In that case, you can call the methods on the
DataTableExtensions class to get a DataTable/DataView instance.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Using LINQ, I can bind my IEnumerable to my ComboBox no problem.

However, in the past it would be a DataTable that I could insert a new
row (e.g. "(Please select...)") and then sort it with a DataView and
ultimately bind the DataView to the ComboBox.

So, how can I do this in LINQ?

Do I need to somehow convert the IEnumerable to a DataTable first and
do it like I used to? If so, how?

Or, can I manipulate IEnumerable more than I think I can? I'm pretty
new to it so don't know really what I can do with it.

Thanks for any help,
Ron
 
B

Ben Voigt [C++ MVP]

Nicholas Paldino said:
Ronald,

Let's say you have a list of states from some source.:

IEnumerable<string> states = ;

And you want to add one more:

states = states.Union(new string[] {"All States"});

Then states will have the fifty states, and an entry at the end "All
States".

Is that really a union, or simply concatenation? IOW, if you ran that
statement twice times, how many instances of the element "All States" would
be returned?
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
Thanks Nicholas.. very helpful! You wouldn't happen to have an example
of the union, would you?



Nicholas Paldino said:
Ron,

Well, you could just union the results of one enumerable with another
which produces just one element. This would give one enumerable that
you can bind to for the complete list. See the Union extension method
on the Enumerable class for more details.

If you really want it in a DataTble, then there is no easy way to
convert what is returned from IEnumerable to a DataTable, unless it is
an IEnumerable<DataRow>. In that case, you can call the methods on the
DataTableExtensions class to get a DataTable/DataView instance.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Using LINQ, I can bind my IEnumerable to my ComboBox no problem.

However, in the past it would be a DataTable that I could insert a new
row (e.g. "(Please select...)") and then sort it with a DataView and
ultimately bind the DataView to the ComboBox.

So, how can I do this in LINQ?

Do I need to somehow convert the IEnumerable to a DataTable first and
do it like I used to? If so, how?

Or, can I manipulate IEnumerable more than I think I can? I'm pretty
new to it so don't know really what I can do with it.

Thanks for any help,
Ron
 
N

Nicholas Paldino [.NET/C# MVP]

Ben,

Well, in this case, the output of a union and a concatenation would be
the same, but you are right, if you added another call to Union, then you
would only have a single item (or should, at least).

Concat would probably be better here, since you already know the
elements are unique, and the Union extension method wouldn't have to
enumerate through the entire list to find only the unique elements between
the two.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ben Voigt said:
Nicholas Paldino said:
Ronald,

Let's say you have a list of states from some source.:

IEnumerable<string> states = ;

And you want to add one more:

states = states.Union(new string[] {"All States"});

Then states will have the fifty states, and an entry at the end "All
States".

Is that really a union, or simply concatenation? IOW, if you ran that
statement twice times, how many instances of the element "All States"
would be returned?
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
Thanks Nicholas.. very helpful! You wouldn't happen to have an example
of the union, would you?



in message Ron,

Well, you could just union the results of one enumerable with
another which produces just one element. This would give one
enumerable that you can bind to for the complete list. See the Union
extension method on the Enumerable class for more details.

If you really want it in a DataTble, then there is no easy way to
convert what is returned from IEnumerable to a DataTable, unless it is
an IEnumerable<DataRow>. In that case, you can call the methods on the
DataTableExtensions class to get a DataTable/DataView instance.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Using LINQ, I can bind my IEnumerable to my ComboBox no problem.

However, in the past it would be a DataTable that I could insert a new
row (e.g. "(Please select...)") and then sort it with a DataView and
ultimately bind the DataView to the ComboBox.

So, how can I do this in LINQ?

Do I need to somehow convert the IEnumerable to a DataTable first and
do it like I used to? If so, how?

Or, can I manipulate IEnumerable more than I think I can? I'm pretty
new to it so don't know really what I can do with it.

Thanks for any help,
Ron
 
R

Ronald S. Cook

Guys, you've been real helpful. I have the below but I think I'm only
close.

First of all, for IEnumerable2 I'm just wanting to hard-code a select of one
record where ID=empty guid, and Value="(Please select...)". I can do this
in SQL but don't know how to do it here.

Any help is greatly appreciated... thanks.

Dim _IEnumerable1 As IEnumerable(Of IEnumerable) = _
From lk In dc.Lookups _
Where lk.LookupGroup = LookupGroup _
Select lk.LookupId, _
lk.LookupCode, _
lk.LookupValue

Dim _IEnumerable2 As IEnumerable(Of IEnumerable) = _
From lk In dc.Lookups _
Where lk.LookupGroup = LookupGroup _
Select lk.LookupId, _
lk.LookupCode, _
lk.LookupValue

Dim _IEnumerable3 As IEnumerable(Of IEnumerable) = _
_IEnumerable1.Concat(_IEnumerable2)

Return _IEnumerable1



Nicholas Paldino said:
Ben,

Well, in this case, the output of a union and a concatenation would be
the same, but you are right, if you added another call to Union, then you
would only have a single item (or should, at least).

Concat would probably be better here, since you already know the
elements are unique, and the Union extension method wouldn't have to
enumerate through the entire list to find only the unique elements between
the two.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ben Voigt said:
Nicholas Paldino said:
Ronald,

Let's say you have a list of states from some source.:

IEnumerable<string> states = ;

And you want to add one more:

states = states.Union(new string[] {"All States"});

Then states will have the fifty states, and an entry at the end "All
States".

Is that really a union, or simply concatenation? IOW, if you ran that
statement twice times, how many instances of the element "All States"
would be returned?
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks Nicholas.. very helpful! You wouldn't happen to have an example
of the union, would you?



"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote in message Ron,

Well, you could just union the results of one enumerable with
another which produces just one element. This would give one
enumerable that you can bind to for the complete list. See the Union
extension method on the Enumerable class for more details.

If you really want it in a DataTble, then there is no easy way to
convert what is returned from IEnumerable to a DataTable, unless it is
an IEnumerable<DataRow>. In that case, you can call the methods on
the DataTableExtensions class to get a DataTable/DataView instance.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Using LINQ, I can bind my IEnumerable to my ComboBox no problem.

However, in the past it would be a DataTable that I could insert a
new row (e.g. "(Please select...)") and then sort it with a DataView
and ultimately bind the DataView to the ComboBox.

So, how can I do this in LINQ?

Do I need to somehow convert the IEnumerable to a DataTable first and
do it like I used to? If so, how?

Or, can I manipulate IEnumerable more than I think I can? I'm pretty
new to it so don't know really what I can do with it.

Thanks for any help,
Ron
 
R

Ronald S. Cook

Thanks guys for all the guidance. In the below, for _IEnumerable2, I'm just
trying to hard-code one record with "(Please Select)" for a value. I can do
that easily in SQL but don't know how here.

Any contunued help very much appreciated... Ron

Dim _IEnumerable1 As IEnumerable(Of IEnumerable) = _
From lk In dc.Lookups _
Where lk.LookupGroup = LookupGroup _
Select lk.LookupId, _
lk.LookupCode, _
lk.LookupValue

Dim _IEnumerable2 As IEnumerable(Of IEnumerable) = _
From lk In dc.Lookups _
Where lk.LookupGroup = LookupGroup _
Select lk.LookupId, _
lk.LookupCode, _
lk.LookupValue

Dim _IEnumerable3 As IEnumerable(Of IEnumerable) = _
_IEnumerable1.Concat(_IEnumerable2)

Return _IEnumerable1



Nicholas Paldino said:
Ben,

Well, in this case, the output of a union and a concatenation would be
the same, but you are right, if you added another call to Union, then you
would only have a single item (or should, at least).

Concat would probably be better here, since you already know the
elements are unique, and the Union extension method wouldn't have to
enumerate through the entire list to find only the unique elements between
the two.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ben Voigt said:
Nicholas Paldino said:
Ronald,

Let's say you have a list of states from some source.:

IEnumerable<string> states = ;

And you want to add one more:

states = states.Union(new string[] {"All States"});

Then states will have the fifty states, and an entry at the end "All
States".

Is that really a union, or simply concatenation? IOW, if you ran that
statement twice times, how many instances of the element "All States"
would be returned?
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks Nicholas.. very helpful! You wouldn't happen to have an example
of the union, would you?



"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote in message Ron,

Well, you could just union the results of one enumerable with
another which produces just one element. This would give one
enumerable that you can bind to for the complete list. See the Union
extension method on the Enumerable class for more details.

If you really want it in a DataTble, then there is no easy way to
convert what is returned from IEnumerable to a DataTable, unless it is
an IEnumerable<DataRow>. In that case, you can call the methods on
the DataTableExtensions class to get a DataTable/DataView instance.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Using LINQ, I can bind my IEnumerable to my ComboBox no problem.

However, in the past it would be a DataTable that I could insert a
new row (e.g. "(Please select...)") and then sort it with a DataView
and ultimately bind the DataView to the ComboBox.

So, how can I do this in LINQ?

Do I need to somehow convert the IEnumerable to a DataTable first and
do it like I used to? If so, how?

Or, can I manipulate IEnumerable more than I think I can? I'm pretty
new to it so don't know really what I can do with it.

Thanks for any help,
Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top