8,497
社区成员
发帖
与我相关
我的任务
分享
//IEnumerable<DataRow> x = from result in dt.AsEnumerable()
// select result;
//if (typeid > 0)
// x = x.Where(p => p.Field<int>("typeid") == typeid);
//if (quest != "")
//{
// string[] arr = quest.Split('|');
// if (arr.Length > 2)
// {
// if (arr[1] != "")
// x = x.Where(p => p.Field<string>(arr[0]).Contains(arr[1]));
// else
// x = x.Where(p => p.Field<string>(arr[0]) != "");
// }
// else
// x = x.Where(p => p.Field<string>(arr[0]) != "");
//}
//if (typeid > 0)
// x = x.Where(p => p.Field<int>.typeid == typeid);
//if (quest != "")
//{
// string[] arr = quest.Split('|');
// if (arr.Length > 2)
// {
// if (arr[1] != "")
// x = x.Where(p => p.Field<string>(arr[0]).Contains(arr[1]));
// else
// x = x.Where(p => p.Field<string>(arr[0]) != "");
// }
// else
// x = x.Where(p => p.Field<string>(arr[0]) != "");
//}
//dt = trans.ToDataTable(x,dt.Rows[0]);
var x = (from result in dt.AsEnumerable()
select new
{
id = result.Field<int>("id"),
categoryid = result.Field<int>("categoryid"),
typeid = result.Field<int>("typeid"),
title = result.Field<string>("title"),
author = result.Field<string>("author"),
sourcesite = result.Field<string>("sourcesite"),
sourceurl = result.Field<string>("sourceurl"),
summary = result.Field<string>("summary"),
ext_fields = result.Field<string>("ext_fields"),
images = result.Field<string>("images"),
m_details = result.Field<string>("m_details"),
keywords = result.Field<string>("keywords"),
addtime = result.Field<int>("m_details")
}).ToList();
public DataTable ToDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
if (varlist == null)
return dtReturn;
foreach (T rec in varlist)
{
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
(rec, null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}
//if (typeid > 0)
// x = x.Where(p => p.Field<int>("typeid") == typeid);
//if (quest != "")
//{
// string[] arr = quest.Split('|');
// if (arr.Length > 2)
// {
// if (arr[1] != "")
// x = x.Where(p => p.Field<string>(arr[0]).Contains(arr[1]));
// else
// x = x.Where(p => p.Field<string>(arr[0]) != "");
// }
// else
// x = x.Where(p => p.Field<string>(arr[0]) != "");
//}
两个查询需要实现,又不能直接写在Linq语句里面,因为我需要判断是否用到这个查询
public void Test()
{
List<Miss> missList = new List<Miss>() {
new Miss() { Id = 1, Name = "1" },
new Miss() { Id = 2, Name = "2" },
new Miss() { Id = 4, Name = "4" },
new Miss() { Id = 5, Name = "5" },
new Miss() { Id = 3, Name = "3" }
};
var mm = from mis in missList
where mis.Id > 2
select mis;
DataTable dt = ToDataTable(mm.ToList());
string name = dt.Rows[0]["Name"].ToString();
var x = from m in dt.AsEnumerable()
select new
{
id = m.Field<int>("Id"),
Name = m.Field<string>("Name"),
};
foreach( var i in x)
{
Response.Write(string.Format("id:{0},name:{1} ",i.id,i.Name));
}
}
输出正常。
没有你说的问题啊
//if (typeid > 0)
// x = x.Where(p => p.Field<int>("typeid") == typeid);
//if (quest != "")
//{
// string[] arr = quest.Split('|');
// if (arr.Length > 2)
// {
// if (arr[1] != "")
// x = x.Where(p => p.Field<string>(arr[0]).Contains(arr[1]));
// else
// x = x.Where(p => p.Field<string>(arr[0]) != "");
// }
// else
// x = x.Where(p => p.Field<string>(arr[0]) != "");
//}
两个查询需要实现,又不能直接写在Linq语句里面,因为我需要判断是否用到这个查询[/quote]
搜 linq 查询表达式目录树
http://developer.51cto.com/art/200909/151787.htm[/quote]
很抱歉,我又看得晕头转向。以我靠昨天一天理解的Linq知识,这种深层次的,我完全看不懂。
我现在只求能有效地针对Datatable进行操作,能够相关查询,并且转回Datatable无误就OK了。因为项目比较赶。
//if (typeid > 0)
// x = x.Where(p => p.Field<int>("typeid") == typeid);
//if (quest != "")
//{
// string[] arr = quest.Split('|');
// if (arr.Length > 2)
// {
// if (arr[1] != "")
// x = x.Where(p => p.Field<string>(arr[0]).Contains(arr[1]));
// else
// x = x.Where(p => p.Field<string>(arr[0]) != "");
// }
// else
// x = x.Where(p => p.Field<string>(arr[0]) != "");
//}
两个查询需要实现,又不能直接写在Linq语句里面,因为我需要判断是否用到这个查询[/quote]
搜 linq 查询表达式目录树
http://developer.51cto.com/art/200909/151787.htm