如何在Visual Studio 2008中将LINQ查询转换为数据表 [英] how to convert linq query to datatable in visual studio 2008

查看:68
本文介绍了如何在Visual Studio 2008中将LINQ查询转换为数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我是linq的新手,所以想知道如何将linq查询数据转换为数据表.我正在使用Visual Studio2008.我尝试了一些解决方案,但无法将CopyToDatable()访问到代码中.我必须在editindexing上使用linq在行本身中在gridview中执行编辑.我必须编辑下拉列表,即城市字段.

Hi!
I am new in linq so want to know that how to get linq query data into datatable. I am using visual studio 2008. I have tried some solution but can not access CopyToDatable() into code. I have to perform edit in gridview using linq in rows itself on editindexing. I have to edit dropdown which is city field.

推荐答案

您的select语句返回的是字符串序列(IEnumerable IQueryable),而不是字符串序列DataRows. CopyToDataTable()仅在T是DataRow或从DataRow派生的IEnumerable<t></t>上可用.
因此,使用select obj代替select new {...}.试试这个:
Your select statement is returning a sequence of strings (IEnumerable or IQueryable), not a sequence of DataRows. CopyToDataTable() is only available on IEnumerable<t></t> where T is or derives from DataRow.
So, instead of select new {...} use select obj. Try this:
var query = from obj in dt.AsEnumerable() where obj.Field<string>("somefield") == "test"
            select obj;
if(query.Count() > 0)
{
    DataTable dt = query.CopyToDataTable();
}




--Amit




--Amit


参考:
通过查询创建数据表(LINQ到数据集) [最佳做法:将LINQ查询结果转换为数据表 [ ^ ]
将Linq查询结果转换为DataTable [ Linq查询到数据表 [此处 [
Refer:
Creating a DataTable From a Query (LINQ to DataSet)[^]
Best Practice: Convert LINQ Query result to a DataTable[^]
Convert Linq Query Result To DataTable[^]
Linq Query to DataTable[^]

..and more similar threads here[^]



如果您要在linq中使用datable,可以选择
在linq中使用数据集
选择您的项目--->单击newitem->选择新建项目--->选择DataTemplate ----->选择数据集--->单击确定.
现在数据集进入您的模板.
然后连接到您的数据库


然后使用以下语法从数据集转换为数据表:



DataSet ds = new DataSet();
DataTable dT = ds.Tables ["TableName"];

if u want datable in linq for that you can use Dataset in linq by choosing

select your project --->click on newitem -->select New Item --->Select DataTemplate ---->Select DataSet --->Click on ok.
Now dataset comes into your template.
then connect to your database


then convert from data set to datatable using below syntax :



DataSet ds = new DataSet();
DataTable dT = ds.Tables["TableName"];


这篇关于如何在Visual Studio 2008中将LINQ查询转换为数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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