Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
This is my function i want select particular column from dataset using lambda expression and add into var temp to return that.I am not getting string format but i am getting object format as System.Collections.Generic.List`1[System.String].Why this is happening?Is my approach right?How can i convert to string?

var temp = new
{
C#
val1= dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<string>("Name")).ToList(),

C#
val2 = dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<int>("TypeId")).ToList()

};
return temp;

Thanks in Advance :-)

What I have tried:

I have tried the above code mentioned.
Posted
Updated 2-Aug-16 1:20am

var temp = new
{

val1 = dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<string>("Name")).FirstOrDefault(),
val2 = dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<int>("TypeId")).FirstOrDefault()
};
 
Share this answer
 
Comments
VICK 3-Aug-16 1:18am    
Copy paste from the Karthik's Solution.
I wrote this straight out of mind, not tested, but I hope you get the idea.

C#
var temp = from t in dataset.Tables[1].Rows() 
            where Convert.ToInt32(t["Id"]) == 1000
            select new { t["Name"].ToString(), t["TypeId"].ToString() };
 
Share this answer
 
use Enumerable.First(TSource) [^]
C#
 var temp = new
{

    val1 = dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<string>("Name")).First(),
    val2 = dataset.Tables[1].AsEnumerable().Where(z => z.Field<int>("Id") == 1000).Select(x => x.Field<int>("TypeId")).First()
};</int></int></string></int>

now val1 will be of type string and val2 as int

better way
C#
DataRow row = dataset.Tables[1].AsEnumerable().FirstOrDefault(z => z.Field<int>("Id") == 1000);
       if (row != null)
       {
           var temp = new   {  val1 = row.Field<string>("Name"), val2 = row.Field<string>("Name"),   };
       }</string></string></int>
 
Share this answer
 
Comments
Maniraj.M 2-Aug-16 7:09am    
Yes its worked but if there is no row for Id 1000 then exception getting.How to manage this?
Karthik_Mahalingam 2-Aug-16 9:31am    
this will work.
DataRow row = dataset.Tables[1].AsEnumerable().FirstOrDefault(z => z.Field<int>("Id") == 1000);
if (row != null)
{
var temp = new { val1 = row.Field<string>("Name"), val2 = row.Field<string>("Name"), };
}

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