Solved

Accessing IEnumerable elements or querying and query

Posted on 2008-10-29
11
1,910 Views
Last Modified: 2013-12-17
Hi Experts.

Hopefully this is a quick one.
My application has two layers - application and data

The data layer in this case returns IEnumerable<Users> collection from the Users table. The collection has multiple fields as they are stored in the Users table.

So now I have this IEnumerable interface/collection with all User related fields but I needI only  to pull the collection of userIDs and UserNames so later they can be assigned to the dropDownList.

I can't figure out how to pull just those two elements from the entire collection of stuff.
Any tips?

Keep in mind please that LInq DataContext classes are stored in the data layer and cannot be access from the program so something like this does not work:
var userNames = from u in userIEnumerable
                            select u.Name;

I hope that I was clear, please let me know if something wasn't.
0
Comment
Question by:techsuppoprt
  • 6
  • 5
11 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi techsuppoprt;

Something like the code snippet should work.

Fernando

IEnumerable<Users> users = //Retern from the Data layer;
 

comboBox1.DataSource = (from u in users select new { ID = u.userID, Name = u.UserName}).ToList();

comboBox1.DisplayMember = "Name";

comboBox1.ValueMember = "ID";

Open in new window

0
 
LVL 1

Author Comment

by:techsuppoprt
Comment Utility
Thank you Fernando, awesome approach.

This could work great but I rand into a bit of a problem.
I'm pulling my IEnumerable collection into the variable and then trying to do what you've posted above:

 var users = usersTable.Get(); //UsersTable.Get() returns IEnumerable

then trying to do what you said:
comboBox1.DataSource = (from u in users select new { ID = u.userID, Name = u.UserName}).ToList();

but get an error message:
Error      1      Could not find an implementation of the query pattern for source type 'System.Collections.IEnumerable'.  'Select' not found.  Consider explicitly specifying the type of the range variable 'u'.      

So I guess instead of var I need to specify the exact type.

I'm not sure what type to specify since my User type class isn't available here...?

0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
You use this line of code and state it returns an IEnumerable
var users = usersTable.Get(); //UsersTable.Get() returns IEnumerable

Is it IEnumerable or IEnumerable<Users> ?

Can you post the code for, usersTable.Get()?
0
 
LVL 1

Author Comment

by:techsuppoprt
Comment Utility

        public IEnumerable<User> GetUser(int _userId)

        {

            IQueryable<User> userQuery;
 

            try

            {

                userQuery= from u in Users

                           where u.Id.Equals(_userId)

                           select u;

            }

            catch (Exception ex)

            {

                throw ex;

            }

            return userQuery.AsEnumerable();

        }

Open in new window

0
 
LVL 1

Author Comment

by:techsuppoprt
Comment Utility
Or actually ... I should mention I don't access that LINQ mehtod directly.
I go through the middle, sort of a "query organizer" class.

between my DataLayer and the Form I also have the Users class. I guess its a bit complicated to explain but it goes like this:

My app:
 |
V

namespace LinkToDb
class Users
{
public IEnumerable Get(int _userId)
 {
  return LinqDataContext.GetUser(_userId);
}

  ||
 V

Data Layer with DataContext and LINQ stuff


I hope I didn't confuse you.. let me know if I did.
Thank you.
}
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
What happens when you change this line:

 var users = usersTable.Get();

To this.

IEnumerable<User> users = usersTable.Get();
0
 
LVL 1

Author Comment

by:techsuppoprt
Comment Utility
That's the thing... I wouldn't have to go though all this is "User" LINQ class was available on this level.
I would have just queried for Name and Id directly through the LINQ query.

The bottom line is I have IEnumerable<User> returned from the Data layer and I need to figure out what type can accept it to query and use its fields to throw in the DropDown control because just var apparently isn't good enough.

I tried IEnumerable<object> but it failed.

What if I change the data layer to return IQueryable or some other collection with the User table info instead  if IEnumerable maybe ?
0
 
LVL 1

Author Comment

by:techsuppoprt
Comment Utility
Morning Fernando.
So I got it to work by modifying my LinkToDb layer

It started working as soon as I replaced

public IEnumerable Get(int _userId)
 {
  return LinqDataContext.GetUser(_userId);
}

with

public IEnumerable<MyLINQNamespace.User> Get(int _userId)
 {
  return LinqDataContext.GetUser(_userId);
}

Not very happy with it but oh well...

Let me know if you can think of any other solutions otherwise I'll just accept your answer above.

Thank you.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
I guess the problem that you were haveing is that IEnumerable is not supported without converting it to IEnumerable<T> first. IEnumerable does not support Linq does not implement methods such as Select, Where, Order By and others as IEnumerable<T> does. That was one of my concerns on a previous post where I asked about it.

I cannot think of another way to do this.
0
 
LVL 1

Author Closing Comment

by:techsuppoprt
Comment Utility
Thank you, helpful as always.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem and as always glad I was able to help.  ;=)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now