none
dataset to a list which contains a class RRS feed

  • Question

  • Hello everyone, I created the following class:

    class clsFields
        {
            // Data Members
            private string _username;
            private string _displayName;

            // Properties
            public string userName
            {
                get
                {
                    return _userName;
                }
                set
                {
                    _userName = value;
                }
            }

            public string displayName
            {
                get
                {
                    return _displayName;
                }
                set
                {
                    _displayName = value;
                }
            }
        }


    Then I fill a dataset from a table and I want to populate only the first property (username) of the class for each object in the List containing clsFields objects:

    ...

    da.Fill(ds, "Default");


    List<clsFields> FieldList = (from g in ds.Tables["Default"].AsEnumerable()
                                         select g.Field<string>("UserName")).ToList();

     

    Now I am getting an error (squiggly line in the "ToList()"):

    Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<MyProject.classes.clsFields>'

    I understand it is because it does not know that I want to map the "UserName" from the table to the username property in the List of type clsFields.  My question is, what would be the correct LINQ to perform this mapping successfully.

     

    Thanks Before Hand,
    Adiel

     

    Friday, October 15, 2010 6:27 PM

Answers

  • Hi Adiel,

    You can do something like this:

    List<clsFields> FieldList = (from g in ds.Tables["Default"].AsEnumerable()
            select new clsFields
            {
             userName = g.Field<string>("UserName")
            }).ToList();
    


    Best regards,

    Alex Liang

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by adiel Monday, October 18, 2010 3:59 PM
    Monday, October 18, 2010 3:07 AM
    Moderator

All replies

  • You are trying to put a list of strings in a list of clsFields, so put

    List<string> FieldList = (from g in ds.Tables["Default"].AsEnumerable()
                                         select g.Field<string>("UserName")).ToList();

    instead

    Regards

    Friday, October 15, 2010 7:05 PM
  • Serguey123 thank you for your reply. I am familiar with how to put the objects into a list of string.  That will not help me.  I need to put the objects into the list of class as shown above:

    List<clsFields> FieldList = (from g in ds.Tables["Default"].AsEnumerable()
                                         select g.Field<string>("UserName")).ToList();

     

    This part should stay how I wrote it:

    List<clsFields> FieldList

    This is where the problem is:

    (from g in ds.Tables["Default"].AsEnumerable()
    select g.Field<string>("UserName")).ToList();

    Thank You,
    Adiel

     

     

    Friday, October 15, 2010 7:17 PM
  • However you are selecting only a string so you will get a list of strings...

    I'm leaving in 5 minutes so I'll be brief

    Either select/construct the whole object or use a loop to incorporate the pulled list of strings into a list of clsFields

    Regards

    Friday, October 15, 2010 7:23 PM
  • Serguey, thanks for your reply.  I am familiar with how to do this with a loop in C# (looping through a dataset and filling the objects in the list), but I want to do it using LINQ. Thus the reason I posted this new question in the forum.  Basically I am looking to see what the correct syntax would be to fill just the UserName property of the list of class called clsField as I mentioned above:

    List<clsFields> FieldList = (from g in ds.Tables["Default"].AsEnumerable()
                                         select g.Field<string>("UserName")).ToList();

     

    This part should stay how I wrote it:

    List<clsFields> FieldList

    This is where the problem is:

    (from g in ds.Tables["Default"].AsEnumerable()
    select g.Field<string>("UserName")).ToList();

    For example:

    Lets say the dataset contains 3 records: Bob, Joe and Mary.

    Once this line executes:

    List<clsFields> FieldList = (from...  <<correct LINQ goes here >>

    I would have a list of class clsFields as follows:

    FieldList[0].userName = "Bob"
    FieldList[0].displayName would be null since it was not initialized

    FieldList[1].userName = "Joe"
    FieldList[1].displayName would be null since it was not initialized

    FieldList[2].userName = "Mary"
    FieldList[2].displayName would be null since it was not initialized

     

    Thank You,
    Adiel

     


    Friday, October 15, 2010 7:37 PM
  • Hi Adiel,

    You can do something like this:

    List<clsFields> FieldList = (from g in ds.Tables["Default"].AsEnumerable()
            select new clsFields
            {
             userName = g.Field<string>("UserName")
            }).ToList();
    


    Best regards,

    Alex Liang

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by adiel Monday, October 18, 2010 3:59 PM
    Monday, October 18, 2010 3:07 AM
    Moderator
  • Wow, AMAZING.  Alex, that is exactly what I was looking for.  It worked perfect.

    Thank You,
    Adiel

    Monday, October 18, 2010 3:58 PM