Solved

Accessing IEnumerable elements or querying and query

Posted on 2008-10-29
11
1,920 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 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

838 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