Results 1 to 13 of 13

Thread: Trying to work with objects ... how to access and filter list

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Trying to work with objects ... how to access and filter list

    I am trying to be a good chap and work with objects. So, I am returning a list of Users (with GroupIDs) to the front end from my DataAccess class ...

    Code:
    public List<DataObjects.User> getUsers()
        {
            List<DataObjects.User> myUserList = new List<DataObjects.User>();
            SqlConnection myConnection = new SqlConnection(connectionString);
            SqlCommand myCommand = myConnection.CreateCommand();
            SqlDataReader dr;
            myCommand.CommandText = "Users_List";
            myCommand.CommandTimeout = 120;
            myCommand.Connection = myConnection;
            myCommand.CommandType = CommandType.StoredProcedure;
    
            myConnection.Open();
            dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
    
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    DataObjects.User myUser = new DataObjects.User();
                    myUser.UserID = dr["UserID"].ToString();
    	myUser.UserName = dr["UserName"].ToString();
                    myUser.GroupID = dr["GroupID"].ToString();
                    myUserList.Add(myUser);
                }
            }
    
            return myUserList;
        }
    In the front end I am populating a Gridview with a list of Users (from a separate DataSource. As I go through the Gridview - in certain conditions - I want to be able to retrieve which GroupIDs the User belongs to from the list above.

    How do I access the list in the front end. I have used Lists before but in the front end I have always used them as the DataSource for a control. Now I just want to be able to filter the List and retrieve the filtered data from the list. How do I do this? Thanks for any help.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Trying to work with objects ... how to access and filter list

    Hello,

    When you say the "front end" are you talking about using JavaScript/jQuery?

    If so, can you elaborate on exactly what you are trying to do, as I am not sure that I follow.

    Gary

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Trying to work with objects ... how to access and filter list

    When I say 'front end' I mean - in C# code on an .aspx.cs page.

    If you take the code above, which is in my Data Access class, I might, for example, call getUsers() (which contains a list of User objects) from a .aspx.cs page to populate a dropdownlist.

    protected void Page_Load(object sender, EventArgs e)
    {
    DataAccess da = new DataAccess();
    ddlUsers.DataSource = da.getUsers();
    ddlUsers.DataBind();
    }

    But, I don't want to use the list of User objects as a datasource for a control - I want to (for example) in the _RowDataBound event of a GridView (which is populated from a separate data source which has UserID in its DataKeyNames collection) to be able to 'pass' a UserID to the List containing the User objects so that the List is filtered by UserID - so I can access the properties contained in the list of User objects about a specific User.

    I do have this working at the moment - but not as I wanted it to work. At the moment, instead of creating a list of User objects I have just returned a recordset of the users and used it as the DataSource for a DataView. I am then using the RowFilter method to filter the DataView to get at the rows that relate to a specific user.

    So, instead of using a DataView and RowFilter to filter it - I want to use the List of User objects and filter that list. That's the bit I'm stuck on - I can't work out how to 'call' the list from the .aspx.cs page and how to filter it.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Trying to work with objects ... how to access and filter list

    Hello,

    Ok, just for clarity sake, lets make sure that we are using the same terminology.

    As you will know the life cycle of an ASP.Net Web Page is split into two parts. There is the life of the page that happens on the client side (the front end) and there is the life of the page that happens on the server side. Your server side code (i.e. the C# code) is executed when the page is first requested, and also when the page is "posted" back to the server via a button click, etc.

    However, once a page is rendered to the client, you can actually still have code executing, but this is done using JavaScript/jQuery on the client. Your C# code can NEVER run on the client side.

    Ok, I "think" I see where you are coming from. Depending on the situation, and whether you want to maintain lots of information in your application, i.e. either in the Cache or in a Session variable, you have two main options. In you DAL, you include a method to GetUserById. Meaning, that on top of your GetAllUsers call, you provide another method which solely returns the user of interest. In order to do this, you either need to go back to the database and grab him, or you need to index to your already existing collection and find the entry that you want.

    You really need to move away from the concept of thinking that this "list" is tired to a particular UI element.

    Gary

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Trying to work with objects ... how to access and filter list

    Thanks for your reply. We do have slightly different terminology. To me 'back end' is the database and database servers and 'front end' is the app that retrieves and displays the data to a user running on web servers. In my terminology, browser is 'client side'.

    You said ... "you either need to go back to the database and grab him, or you need to index to your already existing collection and find the entry that you want."

    That's it - I don't want to go back to the database say 20 times whilst populating a Gridview to get further info about specific users - so I have (at the moment) put the 'extra' data into a Dataview and am filtering this to retrieve data I want about each user.

    My question is ... I don't really want to use a dataview - I'm trying to do everything using objects ... so, I have a List containing the 'extra' User data - but I can't work out how to reference this List and filter it (in the code on the .aspx.cs page)

    Thanks again.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Trying to work with objects ... how to access and filter list

    Hello,

    I have a very long list of threads that I have meant to follow up with, and this is one of them

    Sorry for taking so long to reply!!

    Are you still having issues, or have you moved on?

    Thanks

    Gary

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Trying to work with objects ... how to access and filter list

    Quote Originally Posted by gep13 View Post
    Hello,

    I have a very long list of threads that I have meant to follow up with, and this is one of them

    Sorry for taking so long to reply!!

    Are you still having issues, or have you moved on?

    Thanks

    Gary
    In a sense I've moved on - it works fine as it is - that is by filtering a Dataview.

    I'd still like to work out how to filter a list though.

    Take this for an example:

    Code:
    protected void GetGroupMembers()
        {
            DataAccess da = new DataAccess();
            List<DataObjects.UserGroupDetails> myUserGroupList = new List<DataObjects.UserGroupDetails>();
            myUserGroupList = da.getGroupMembers_List_ByUserGroup(Convert.ToInt32(Session["UserGroupID"].ToString()));
            gvMembers.DataSource = myUserGroupList;
            gvMembers.DataKeyNames = new string[] { "UserID", "UserName", "UserGroupID", "GroupID", "Role", "CanEdit" };
            gvMembers.DataBind();
        }
    This returns a list of the users in a group or, more specifically, a list of 'UserDetail' objects.

    In the code above I am using that list as the datasource of a gridview.

    But what if I just want to create the list object and then, while looping through some other data that includes UserIDs - use the UserID in that other data as a parameter to retrieve just the info about that user from the list. Sorry if I'm not clear.

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Trying to work with objects ... how to access and filter list

    Hello,

    Yip, you lost me

    Can you take another stab at explaining it?

    Gary

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Trying to work with objects ... how to access and filter list

    Hi, and thanks for your perserverance.

    Code:
    DataAccess da = new DataAccess();
            List<DataObjects.UserGroupDetails> myUserGroupList = new List<DataObjects.UserGroupDetails>();
            myUserGroupList = da.getGroupMembers_List_ByUserGroup(Convert.ToInt32(Session["UserGroupID"].ToString()));
    At this point I lhave a List called myUserGroupList - which is a list of the UserGroupDetails object.

    If I wanted to loop through the list I could do this:

    Code:
    for (int i = 0; i < myUserGroupList.Count; i++) 
            {
                string UserName = myUserGroupList[i].UserName.ToString();
                string UserEmail = myUserGroupList[i].UserEmail.ToString();
    int UserID = Convert.ToInt32(myUserGroupList[i].UserID.ToString());
            }
    (Let's say that each UserGroupDetails object contains the UserID, UserName and UserEmail)

    So, let's say I am looping through some other dataset which contains a list of UserIDs (on the same page - within the same function) and when I get to a particular UserID some condition is met that means I want to access the info stored in myUserGroupList about that User (in the UserGroupDetails object in the list).

    I don't want to loop through the list each time until I find the specific UserID I am looking for - I want to filter the list so I can go straight to the correct UserGroupDetails object in the list - so I can easily retrieve the UserName etc for (say) the UserID whose UserID is 3 (or whatever).

    (At the moment I am holding all the data about the users in a DataView and filtering that with rowFilter - which works fine - but it's not an object and I'm trying to do everything in objects.)

    Any clearer?

    Thanks again.
    Last edited by Webskater; Jul 30th, 2011 at 09:06 AM.

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Trying to work with objects ... how to access and filter list

    Ah, ok, I think I see what you are referring to now.

    One way to do this would be to use a Dictionary object. The dictionary would be defined as:

    Code:
    Dictionary<string, DataObjects.UserGroupDetails>
    where the first type (string) would be your User ID. When you are looping through your other dataset, and you find a User Id that you want to "look up" you can index into the Dictionary using the User Id and return the DataObjects.UserGroupDetails directly.

    Hope that makes sense!

    Gary

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Trying to work with objects ... how to access and filter list

    Quote Originally Posted by gep13 View Post
    Ah, ok, I think I see what you are referring to now.

    One way to do this would be to use a Dictionary object. The dictionary would be defined as:

    Code:
    Dictionary<string, DataObjects.UserGroupDetails>
    where the first type (string) would be your User ID. When you are looping through your other dataset, and you find a User Id that you want to "look up" you can index into the Dictionary using the User Id and return the DataObjects.UserGroupDetails directly.

    Hope that makes sense!

    Gary
    Thanks for that Gary. I had a look at it and think it's .net 3.5 or later? I'm stuck on .net 2.0 for this particular app.

    While I was looking around I came upon this ... seems I can filter a list doing this ...

    List<DataObjects.UserGroupDetails> FilteredList = myUserGroupList.FindAll(???What goes here???)

    In the code above where I have written "??? What goes here???" it seems that what is wanted is (according to intellisense is ... (Predicate<DataObjects.UserGroupDetails> match)

    What I'm hoping is that (somehow) I can pass the UserID into (Predicate<DataObjects.UserGroupDetails> match) and that FilteredList will contain the UserGroupDetails DataObject of that particular user.

    Any idea what the syntax should be to pass a UserID in there?

    Thanks again.

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Trying to work with objects ... how to access and filter list

    Hello,

    The Generic Dictionary has been around since .Net 2.0, so there is no reason why you can't use it.

    As a brief example, take a look at the following...

    Assuming we have a UserGroupDetails defined as follows:

    Code:
        public class UserGroupDetails
        {
            public string UserID
            {
                get;
                set;
            }
    
            public string GroupName
            {
                get;
                set;
            }
    
            public string GroupDescription
            {
                get;
                set;
            }
        }
    Then we can do the following to create the Dictionary:

    Code:
            Dictionary<string, UserGroupDetails> UserGroups = new Dictionary<string, UserGroupDetails>();
    
            protected void Page_Load(object sender, EventArgs e)
            {
                UserGroups.Add("UserId1", new UserGroupDetails() { UserID = "UserId1", GroupName = "Group1", GroupDescription = "Description1" });
                UserGroups.Add("UserId2", new UserGroupDetails() { UserID = "UserId2", GroupName = "Group2", GroupDescription = "Description2" });
                UserGroups.Add("UserId3", new UserGroupDetails() { UserID = "UserId3", GroupName = "Group3", GroupDescription = "Description3" });
                UserGroups.Add("UserId4", new UserGroupDetails() { UserID = "UserId4", GroupName = "Group4", GroupDescription = "Description4" });
    }
    You could obviously populate this from your database.

    Then, you access a particular item from the Dictionary, you could use;

    Code:
                UserGroupDetails groupDetail = UserGroups["UserId3"];
    
                Response.Write(String.Format("UserID: {0}, ", groupDetail.UserID));
                Response.Write(String.Format("GroupName: {0}, ", groupDetail.GroupName));
                Response.Write(String.Format("GroupDescription: {0}, ", groupDetail.GroupDescription));
    Notice I am indexing into the Dictionary using the UserID.

    In order to use the technique that you are talking about you would need to create a method in which you would do the work to find the item that you are interested in, then you pass in a delegate to this method, and then, for each item in the collection the method is executed and if the item exists, it will be returned.

    You can find out more details about this here:

    http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx

    Let me know if that doesn't make sense.

    Gary

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Trying to work with objects ... how to access and filter list

    Thanks again for that - I'll give it a go.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width