Solved

Accessing IEnumerable elements or querying and query

Posted on 2008-10-29
11
1,926 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 22834612
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
ID: 22834865
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 63

Expert Comment

by:Fernando Soto
ID: 22835049
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:techsuppoprt
ID: 22835952

        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
ID: 22836061
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22837657
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
ID: 22837981
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
ID: 22843386
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 63

Expert Comment

by:Fernando Soto
ID: 22843621
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
ID: 31511315
Thank you, helpful as always.
0
 
LVL 63

Expert Comment

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

Featured Post

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 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