How add/concatenate "Please Select" row to result set in LINQ?

R

Ronald S. Cook

Sorry to post this here, but there's not much talent on the VB board (yes,
kissing up in hopes for some help).

I'm simply trying to add a "(Please Select)" row to a combobox in a Windows
form. But, since it's LINQ, I'm in unfamiliar territory.

In the below, it seems like I must have (Of Lookup) in order for the .Concat
method to be available.

But when I run it, it says it can't convert _IQueryable2 from type Lookup to
type Anonymoyus.

************************************************************
Dim _IEnumerable As IEnumerable

Dim _IQueryable1 As IQueryable(Of Lookup) = _
From lk In dc.Lookups _
Where lk.LookupCode <> "INA" _
And lk.LookupGroup = LookupGroup

Dim _IQueryable2 As IQueryable(Of Lookup) = _
From lk In dc.Lookups _
Select LookupId = "00000000-0000-0000-0000-000000000000", _
LookupCode = "XXX", _
LookupValue = "(Please Select)"

Dim _IQueryable3 As IQueryable = _
_IQueryable1.Concat(_IQueryable2)

_IEnumerable = _IQueryable3

Return _IEnumerable
************************************************************

Am I on the right track at all? Any guidance would be greatly appreciated.

Thanks,
Ron
 
N

Nicholas Paldino [.NET/C# MVP]

Ron,

I know I alluded to this situation in an earlier post by you, but
looking at it now, it's definitely overthought.

Since you are doing this in Windows Forms, you ^must^ have an IList
implementation to bind to. Because of another earlier post by you, you are
going to have to call ToList on the query to get it into an implementation
of IList that can be bound to.

Instead of placing the "(Please Select)" part in your query, why not
just make a call to the Insert method on the List<T> that is returned to you
after calling ToList and then insert your item at the beginning?

It seems much easier to me than trying to concatenate it to your query
(at least now it does).
 
R

Ronald S. Cook

That insert idea sounds great, Nicholas. Any idea how that syntax would
look? Sorry I'm such a novice to LINQ.



Nicholas Paldino said:
Ron,

I know I alluded to this situation in an earlier post by you, but
looking at it now, it's definitely overthought.

Since you are doing this in Windows Forms, you ^must^ have an IList
implementation to bind to. Because of another earlier post by you, you
are going to have to call ToList on the query to get it into an
implementation of IList that can be bound to.

Instead of placing the "(Please Select)" part in your query, why not
just make a call to the Insert method on the List<T> that is returned to
you after calling ToList and then insert your item at the beginning?

It seems much easier to me than trying to concatenate it to your query
(at least now it does).

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

Ronald S. Cook said:
Sorry to post this here, but there's not much talent on the VB board
(yes, kissing up in hopes for some help).

I'm simply trying to add a "(Please Select)" row to a combobox in a
Windows form. But, since it's LINQ, I'm in unfamiliar territory.

In the below, it seems like I must have (Of Lookup) in order for the
.Concat method to be available.

But when I run it, it says it can't convert _IQueryable2 from type Lookup
to type Anonymoyus.

************************************************************
Dim _IEnumerable As IEnumerable

Dim _IQueryable1 As IQueryable(Of Lookup) = _
From lk In dc.Lookups _
Where lk.LookupCode <> "INA" _
And lk.LookupGroup = LookupGroup

Dim _IQueryable2 As IQueryable(Of Lookup) = _
From lk In dc.Lookups _
Select LookupId = "00000000-0000-0000-0000-000000000000", _
LookupCode = "XXX", _
LookupValue = "(Please Select)"

Dim _IQueryable3 As IQueryable = _
_IQueryable1.Concat(_IQueryable2)

_IEnumerable = _IQueryable3

Return _IEnumerable
************************************************************

Am I on the right track at all? Any guidance would be greatly
appreciated.

Thanks,
Ron
 
N

Nicholas Paldino [.NET/C# MVP]

Ronald,

Well, you have your IEnumerable<T>, which you call the ToList extension
method on, right? So you get a List<T> like so:

IEnumerable<string> query = ...;

IList<string> list = query.ToList();

list.Insert(0, "(Please Select)");

That's really it. You will have to substitute the string type for
whatever type you are enumerating in your query, but that's trivial.

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

Ronald S. Cook said:
That insert idea sounds great, Nicholas. Any idea how that syntax would
look? Sorry I'm such a novice to LINQ.



Nicholas Paldino said:
Ron,

I know I alluded to this situation in an earlier post by you, but
looking at it now, it's definitely overthought.

Since you are doing this in Windows Forms, you ^must^ have an IList
implementation to bind to. Because of another earlier post by you, you
are going to have to call ToList on the query to get it into an
implementation of IList that can be bound to.

Instead of placing the "(Please Select)" part in your query, why not
just make a call to the Insert method on the List<T> that is returned to
you after calling ToList and then insert your item at the beginning?

It seems much easier to me than trying to concatenate it to your query
(at least now it does).

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

Ronald S. Cook said:
Sorry to post this here, but there's not much talent on the VB board
(yes, kissing up in hopes for some help).

I'm simply trying to add a "(Please Select)" row to a combobox in a
Windows form. But, since it's LINQ, I'm in unfamiliar territory.

In the below, it seems like I must have (Of Lookup) in order for the
.Concat method to be available.

But when I run it, it says it can't convert _IQueryable2 from type
Lookup to type Anonymoyus.

************************************************************
Dim _IEnumerable As IEnumerable

Dim _IQueryable1 As IQueryable(Of Lookup) = _
From lk In dc.Lookups _
Where lk.LookupCode <> "INA" _
And lk.LookupGroup = LookupGroup

Dim _IQueryable2 As IQueryable(Of Lookup) = _
From lk In dc.Lookups _
Select LookupId = "00000000-0000-0000-0000-000000000000", _
LookupCode = "XXX", _
LookupValue = "(Please Select)"

Dim _IQueryable3 As IQueryable = _
_IQueryable1.Concat(_IQueryable2)

_IEnumerable = _IQueryable3

Return _IEnumerable
************************************************************

Am I on the right track at all? Any guidance would be greatly
appreciated.

Thanks,
Ron
 
R

Ronald S. Cook

Thanks very much for your help. I'll try it.


Nicholas Paldino said:
Ronald,

Well, you have your IEnumerable<T>, which you call the ToList extension
method on, right? So you get a List<T> like so:

IEnumerable<string> query = ...;

IList<string> list = query.ToList();

list.Insert(0, "(Please Select)");

That's really it. You will have to substitute the string type for
whatever type you are enumerating in your query, but that's trivial.

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

Ronald S. Cook said:
That insert idea sounds great, Nicholas. Any idea how that syntax would
look? Sorry I'm such a novice to LINQ.



Nicholas Paldino said:
Ron,

I know I alluded to this situation in an earlier post by you, but
looking at it now, it's definitely overthought.

Since you are doing this in Windows Forms, you ^must^ have an IList
implementation to bind to. Because of another earlier post by you, you
are going to have to call ToList on the query to get it into an
implementation of IList that can be bound to.

Instead of placing the "(Please Select)" part in your query, why not
just make a call to the Insert method on the List<T> that is returned to
you after calling ToList and then insert your item at the beginning?

It seems much easier to me than trying to concatenate it to your
query (at least now it does).

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

Sorry to post this here, but there's not much talent on the VB board
(yes, kissing up in hopes for some help).

I'm simply trying to add a "(Please Select)" row to a combobox in a
Windows form. But, since it's LINQ, I'm in unfamiliar territory.

In the below, it seems like I must have (Of Lookup) in order for the
.Concat method to be available.

But when I run it, it says it can't convert _IQueryable2 from type
Lookup to type Anonymoyus.

************************************************************
Dim _IEnumerable As IEnumerable

Dim _IQueryable1 As IQueryable(Of Lookup) = _
From lk In dc.Lookups _
Where lk.LookupCode <> "INA" _
And lk.LookupGroup = LookupGroup

Dim _IQueryable2 As IQueryable(Of Lookup) = _
From lk In dc.Lookups _
Select LookupId = "00000000-0000-0000-0000-000000000000", _
LookupCode = "XXX", _
LookupValue = "(Please Select)"

Dim _IQueryable3 As IQueryable = _
_IQueryable1.Concat(_IQueryable2)

_IEnumerable = _IQueryable3

Return _IEnumerable
************************************************************

Am I on the right track at all? Any guidance would be greatly
appreciated.

Thanks,
Ron
 
R

Ronald S. Cook

Nicholas... I got it all working GREAT. Thanks again so much for the
guidance.


Nicholas Paldino said:
Ronald,

Well, you have your IEnumerable<T>, which you call the ToList extension
method on, right? So you get a List<T> like so:

IEnumerable<string> query = ...;

IList<string> list = query.ToList();

list.Insert(0, "(Please Select)");

That's really it. You will have to substitute the string type for
whatever type you are enumerating in your query, but that's trivial.

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

Ronald S. Cook said:
That insert idea sounds great, Nicholas. Any idea how that syntax would
look? Sorry I'm such a novice to LINQ.



Nicholas Paldino said:
Ron,

I know I alluded to this situation in an earlier post by you, but
looking at it now, it's definitely overthought.

Since you are doing this in Windows Forms, you ^must^ have an IList
implementation to bind to. Because of another earlier post by you, you
are going to have to call ToList on the query to get it into an
implementation of IList that can be bound to.

Instead of placing the "(Please Select)" part in your query, why not
just make a call to the Insert method on the List<T> that is returned to
you after calling ToList and then insert your item at the beginning?

It seems much easier to me than trying to concatenate it to your
query (at least now it does).

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

Sorry to post this here, but there's not much talent on the VB board
(yes, kissing up in hopes for some help).

I'm simply trying to add a "(Please Select)" row to a combobox in a
Windows form. But, since it's LINQ, I'm in unfamiliar territory.

In the below, it seems like I must have (Of Lookup) in order for the
.Concat method to be available.

But when I run it, it says it can't convert _IQueryable2 from type
Lookup to type Anonymoyus.

************************************************************
Dim _IEnumerable As IEnumerable

Dim _IQueryable1 As IQueryable(Of Lookup) = _
From lk In dc.Lookups _
Where lk.LookupCode <> "INA" _
And lk.LookupGroup = LookupGroup

Dim _IQueryable2 As IQueryable(Of Lookup) = _
From lk In dc.Lookups _
Select LookupId = "00000000-0000-0000-0000-000000000000", _
LookupCode = "XXX", _
LookupValue = "(Please Select)"

Dim _IQueryable3 As IQueryable = _
_IQueryable1.Concat(_IQueryable2)

_IEnumerable = _IQueryable3

Return _IEnumerable
************************************************************

Am I on the right track at all? Any guidance would be greatly
appreciated.

Thanks,
Ron
 
Top