Solved

Accessing IEnumerable elements or querying and query

Posted on 2008-10-29
11
1,922 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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