LINQ中的简单选择查询 [英] simple select query in linq

查看:109
本文介绍了LINQ中的简单选择查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个学生表,我想显示ID为1的学生.

Lets say I have a student table and I want to display the student with ID 1.

SELECT *
FROM STUDENT ST
WHERE ST.ID = 1

这就是我在Linq中实现这一目标的方式.

This is how I achive this in Linq.

StudentQuery = from r in oStudentDataTable.AsEnumerable()
                                     where (r.Field<int>("ID") == 1)
                                     select r;
            oStudentDataTable = StudentQuery.CopyToDataTable();

但是如果我想显示具有这些ID 1,2,3,4,5的学生该怎么办?

but what if I want to display the students with these ids 1,2,3,4,5..

SELECT *
FROM STUDENT ST
WHERE ST.ID IN (1,2,3,4,5)

如何在Linq中实现这一目标?

How can I achieve this in Linq?

推荐答案

使用 .Contains

var list = new List<int> { 1, 2, 3, 4, 5 };

var result = (from r in oStudentDataTable.AsEnumerable()
              where (list.Contains(r.Field<int>("ID"))
              select r).ToList();

这篇关于LINQ中的简单选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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