首先:添加引用

项目添加引用 System.Data.DataSetExtensions

 注意下面代码 AsEnumerable()方法的使用

代码一

public   static  IEnumerable getEnumerable() 
{
DataTable dt = getDatatable();
try
{
var query = from q in dt.AsEnumerable()
where q.Field < string > ( " IPLocation " ) == " 北京 "
select new
{
IPid = q.Field < int > ( " IPid " ),
IPFrom = q.Field < string > ( " IPFrom " ),
IPTo = q.Field < string > ( " IPTo " ),
IPCity = q.Field < string > ( " IPCity " ),
IPToNumber = q.Field < string > ( " IPToNumber " ),
IPFromNumber = q.Field < string > ( " IPFromNumber " )
};
return query;
}
catch
{
return null ;
}
}

还可以这样:代码二

public   static  IEnumerable getEnumerable()
{
DataTable dt = getDatatable();
try
{
var query = from q in dt.AsEnumerable()
orderby long .Parse(q[ " IPid " ].ToString()) descending
where q[ " IPid " ].ToString() == " 345058 "
select new
{
IPid = q[ " IPid " ].ToString(),
IPFrom = q[ " IPFrom " ].ToString(),
IPTo = q[ " IPTo " ].ToString(),
IPLocation = q[ " IPLocation " ].ToString(),
IPCity = q[ " IPCity " ].ToString(),
IPToNumber = q[ " IPToNumber " ].ToString(),
IPFromNumber = q[ " IPFromNumber " ].ToString()
};
return query;
// List<IPInfo> list = new List<IPInfo>();
// foreach (var q in query)
// {
// IPInfo model = new IPInfo();
// model.IPCity = q.IPCity;
// model.IPFrom = q.IPFrom;
// model.IPFromNumber = q.IPFromNumber;
// model.IPid = long.Parse(q.IPid);
// model.IPLocation = q.IPLocation;
// model.IPTo = q.IPTo;
// model.IPToNumber = q.IPToNumber;
// list.Add(model);
// }
// return list;
}
catch
{
return null ;
}
}