如何从linq获取每列的行值

时间:2016-07-11 13:03:43

标签: .net linq

我对linq很新。我在从一行中提取每列的值时遇到问题。这是我的代码。

var websitedetail = (from x in dt.AsEnumerable()
                    where x["CYBER_TY"].ToString() == "WEBSITE"
                    select x).FirstOrDefault();

        var test2 = websitedetail.Field<int>("CYBER_SERNO");

dt是一个带有来自数据库sp的源的数据表。这给了我一个指定的强制转换无效。我试过convert.toint32

谢谢

2 个答案:

答案 0 :(得分:0)

var websitedetail = (from x in dt.AsEnumerable()
                    where x["CYBER_TY"].ToString() == "WEBSITE"
                    select x).FirstOrDefault();


var test2 = websitedetail.CYBER_SERNO

Linq对象是强类型的;这意味着对象上的属性是行中的列。例如

Database Table FOO:
ID
Name
Description

turnes into

FOO.FirstOrDefault().ID

FOO.FirstOrDefault().Description

答案 1 :(得分:0)

var websitedetail = (from x in dt.AsEnumerable()
                    where x["CYBER_TY"].ToString() == "WEBSITE"
                    select x).select(y=>y.ID).FirstOrDefault();
相关问题