How can I use linq to build an object from 1 row of data?

Posted by Hcabnettek on Stack Overflow See other posts from Stack Overflow or by Hcabnettek
Published on 2010-06-17T15:08:13Z Indexed on 2010/06/17 15:13 UTC
Read the original article Hit count: 255

Hi All,

I have a quick linq question. I have a stored proc that should return one row of data. I would like to use a lambda to build an object. Here's what I'm currently doing which works, but I know I should be able to use First instead of Select except I can't seem to get the syntax correct. Can anyone straighten me out here? Thanks for any help.

 var location = new GeoLocationDC();
 DataSet ds = db.ExecuteDataSet(dbCommand);
 if(ds.Tables[0].Rows.Count == 1)
            {
                 var rows = ds.Tables[0].AsEnumerable();
                 var x = rows.Select(
                     c => new GeoLocationDC
                              {
                                  Latitude = Convert.ToInt32(c.Field<string>("LATITUDE")),
                                  Longitude = Convert.ToInt32(c.Field<string>("LONGITUDE"))
                              }).ToList();
                 if(x.Count > 0 )
                 {
                     location = x[0];
                 }

Cheers, ~ck }

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ