使用 LINQ 从嵌套 SQL 查询中获取结果

发布于 2024-09-02 09:07:22 字数 937 浏览 2 评论 0原文

这是我的第一个问题,也是我在 Linq 的第一天,所以对我来说理解有点困难。 我想从数据库中获取一些记录

,即

select * from tblDepartment 
where department_id in 
(
   select department_id from tblMap 
   where Guest_Id = @GuestId
)

我已经获取了两个数据表。即 tblDepartment,tblMap

现在我想获取这个结果并将其存储在第三个 DataTable 中。

我该怎么办呢。

到目前为止,在谷歌搜索后我已经能够构建这个查询。

var query = from myrow in _dtDepartment.AsEnumerable()
            where myrow.Field<int>("Department_Id") == _departmentId
            select myrow;

请为我提供一些学习 Linq 的链接,主要用于 DataTablesDataSets

编辑:

我有一个非常相似的示例这里但我仍然无法理解它是如何工作的。 请在上面放一些火把。

我想这样做是因为我已经从数据库中获取数据并且当我已经有数据时不想再次发送请求。但我想根据条件过滤这些数据。

This is my first question and first day in Linq so bit difficult day for me to understand.
I want to fetch some records from database

i.e.

select * from tblDepartment 
where department_id in 
(
   select department_id from tblMap 
   where Guest_Id = @GuestId
)

I have taken two DataTable. i.e. tblDepartment, tblMap

Now I want to fetch this result and want to store it in third DataTable.

How can I do this.

I have been able to construct this query up till now after googling.

var query = from myrow in _dtDepartment.AsEnumerable()
            where myrow.Field<int>("Department_Id") == _departmentId
            select myrow;

Please provide me some link for learning Linq mainly for DataTables and DataSets.

EDIT:

I have got a very similar example here but i m still not able to understand how it is working.
Please put some torch on it.

I want to do this because I am already fetching data from database and dont want to send request again when I already have data. But I want to filter that data based on conditions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2024-09-09 09:07:22

您正在尝试的不是实际的 LINQ-To-SQL。这是一个常见的误解。在 LINQ-To-SQL 中,您可以通过简单的方式将一些表映射到实体,然后在运行时框架通过 SQL 转换您的 LINQ 查询,并作为预定义的强类型实体对象返回。 (我使用“实体”作为描述性术语;与实体框架无关。)LINQ-To-SQL 是您执行的操作,而不是 ADO.NET。

您正在做的是一种 LINQ 到对象的风格,通常称为“LINQ 到数据集”,我经常使用它。扩展方法 .AsEnumerable()IEnumerable 对象中返回 DataTable 的 DataRows,这非常方便。但是,使用 LINQ-To-DataTable,您要么 (A) 没有强类型对象,要么 (B) 您自己负责将数据放入强类型对象中当您像往常一样使用 ADO.NET 后,LINQ-to-DataTable 是一种将数据 ADO.NET DataTable 中取出并存入的好方法>其他东西,无论消耗数据的是什么。

上面的 LINQ 查询将获取已经针对数据库运行的 DataTable,并返回一个 IEnumerable,这非常棒。执行:

在 LINQ 语句之后,执行:

For Each DataRow in query
    //Do your stuff
Next

如果此时您确实特别需要 DataTable 对象,则 看看这个。但你真的需要一个数据表吗?

我必须推荐一本关于 LINQ 的非常好的书。 LINQ很糟糕。在一个似乎有太多需要学习的 .NET 世界中,LINQ 值得花时间去真正理解。获取:LINQ 实际操作

What you are attempting is not actual LINQ-To-SQL. It is a common misunderstanding. In LINQ-To-SQL you map some tables to entities in a simple way, then at run-time your LINQ query is translated by SQL by the framework and returned as your pre-defined strongly typed entity objects. (I use "entity" as a descriptive term; nothing to do with Entity Framework.) LINQ-To-SQL is something you do instead of ADO.NET.

What you are doing is a flavor of LINQ-to-object often called "LINQ-To-Dataset", which I use often. The extension method .AsEnumerable() returns a DataTable's DataRows in an IEnumerable<DataRow> object, which is very handy. However, with LINQ-To-DataTable, you either (A) won't have strongly typed objects, or (B) you are taking responsibility for putting your data into strongly typed objects yourself. After you've used ADO.NET like you always have, LINQ-to-DataTable is a great way to get your data out of ADO.NET DataTables and into something else, whatever is consuming the data.

You LINQ query above will take your DataTable, which has already run against the DB, and return an IEnumerable<DataRow>, which is great. Do:

After your LINQ statement, do:

For Each DataRow in query
    //Do your stuff
Next

If you really need a DataTable object in particular at this point, then check this out. But do you really need a DataTable?

And I have to suggest a really great book about LINQ. LINQ rocks. In a .NET world that seems to have too much that we need to learn, LINQ is worth taking the time to really understand. Get: LINQ In Action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文