我有一个快速的 linq 问题。我有一个应该返回一行数据的存储过程。我想使用 lambda 来构建一个对象。这是我目前正在做的工作,但我知道我应该能够使用 First 而不是 Select 除非我似乎无法获得正确的语法。任何人都可以在这里理顺我吗?感谢您的帮助。
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];
}
干杯, ~ck
请您参考如下方法:
您不需要使用 Select - 因为您知道只有 1 行,所以您可以直接使用它:
var location = new GeoLocationDC();
var ds = db.ExecuteDataSet(dbCommand);
if(ds.Tables[0].Rows.Count == 1)
{
var row = ds.Tables[0].AsEnumerable().Single();
location.Latitude = row.Field<int>("LATITUDE");
location.Longitude = row.Field<int>("LONGITUDE");
}
