?
Solved

Accessing IEnumerable elements or querying and query

Posted on 2008-10-29
11
Medium Priority
?
1,928 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 2000 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
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

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:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

765 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