从LINQ结果集中删除列名 [英] Remove Column Names From LINQ Resultset

查看:93
本文介绍了从LINQ结果集中删除列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道我的问题是对的还是我的计划是对的,但是我正在使用jquery数据表插件,并从Json转换后的LINQ结果中获取其datasource.

Don't know if my question is right or my plan is right, but i am using jquery datatable plugin and getting its datasource from a Json converted LINQ result.

我的问题是,我的结果集包含了我认为Jquery DataTable(客户端)不需要的datatable(ado)中的列名称.

My problem was, my resultset includes the name of the columns from my datatable(ado) which I think Jquery DataTable(client) does not require.

是否有可能在将我的LINQ结果转换为json之前不包括或不删除它们的列名?

Is it possible if i dont include or rather remove the column names from my LINQ result before converting it to json?

这是我的服务器代码.

 context.Response.ContentType = "application/json";
 var y = from t in dt select t;
 ret = JsonConvert.SerializeObect(y, Formatting.None);

 context.Response.Write(ret);

上面的代码返回包含所有列名称的结果.我也正在使用JSON.NET

The above code returns a result including all column names. Also i am using JSON.NET

这是我填充dt的方式

dsTableAdapters.dtTableAdapter adap = new dsTableAdapters.dtTableAdapter();
adap.Fill(ds.dt);

输出JSON:

[
[
    "COL1": 1,
    "COL2": "R1C2"
},
{
    "COL1: 2,
    "COL2": "R2C2"
},
{
    "COL1": 3,
    "COL2": "R3C3"
}
]

jquery Datatable插件的必需输出必须为:

The required output based from jquery Datatable plugin must be:

[
[
    1,
    "R1C2"
],
[
    2,
    "R2C2"
],
[
    3,
    "R3C3"
]
]

推荐答案

var y = (from t in dt.AsEnumerable() 
         select new { COL1= t.Field<string>("FirstName"), 
                      COL2 = t.Field<string>("LastName") }).ToList();

假设dt是您的DataTable,并且其中有2个列分别称为FirstName和LastName,那么您只想输出这些列.

Assumming dt is your DataTable and you have 2 columns called FirstName and LastName in your in that and you want to output only those.

这将起作用.经过测试.

This will work. Tested.

http://msdn.microsoft.com/zh-cn/library/system.data.datatableextensions.asenumerable.aspx

DataTable上的AsEnumerable扩展方法将返回DataRows的集合.然后使用必填字段根据您的Linq结果进行投影.由于它是dataRow类型,因此您需要指定每种列类型将是什么.

The AsEnumerable extension method on a DataTable will return a Collection of DataRows. then you do a projection from your Linq Result with required fields. Since it is dataRow type, you need to specify what each column type will be.

这篇关于从LINQ结果集中删除列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆