Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public List<createUserModel> userAvailabilityCheck(createUserModel obj)
        {
            SqlConnection dbConnection = new SqlConnection(conntection);
            SqlCommand cmd = new SqlCommand("select_User", dbConnection);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);

            var query = (from n in ds.Tables["User"].AsEnumerable()
                         where n.Field<string>("userName") == obj.UserName
                         select n);
            List<createUserModel> checkList = query.ToList();
            return checkList;
        }


Getting Error on
C#
List<createUserModel> checkList = query.ToList();

this line.
C#
cannot convert source type system.collections.generic.list<System.Data.DataRow> to target type system.collections.generic.list<createUserModel>

Please Help.
Posted
Updated 2-Mar-22 1:38am
v3
Comments
Kenneth Haugland 2-Feb-15 2:53am    
You just got the row in your query, you need to get the underlying class of the row i.a. the createUserModel.
Sinisa Hajnal 2-Feb-15 3:05am    
The error tells you what is wrong. You're trying to convert DataRow to createUserModel i.e. in your query there are datarows, not createUserModels.

1 solution

Linq doesn't know what types it should use - so when you iterate a DataTable and select items it returns DataRow items which don't have any casting operations to your own classes! Instead, create a createUserModel constructor that accepts a DataRow and select that instead:
C#
var query = (from n in ds.Tables["User"].AsEnumerable()
             where n.Field<string>("userName") == obj.UserName
             select new createUserModel(n));
List<createUserModel> checkList = query.ToList();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900