怎样根据集合中数据筛选表数据

itliyi 2013-11-01 10:40:50
比如有个字符串集合
List<string> list = new ...;

x表中y字段如果有值存在list中,取出x表数据
比较快速方法
请教 谢谢
...全文
114 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
全栈极简 2013-11-01
  • 打赏
  • 举报
回复
List<string> list = new List<string>();
            list.AddRange(new string[] { "1", "2" });

            DataTable dt = new DataTable();
            dt.Columns.Add("X", typeof(int));
            dt.Columns.Add("Y", typeof(string));
            dt.Rows.Add(1, "1");
            dt.Rows.Add(2, "2");
            dt.Rows.Add(3, "3");

            var result = from p in list.AsEnumerable()
                         from q in dt.AsEnumerable()
                         where p == q.Field<string>("Y")
                         select new { X = q.Field<int>("X"), Y = q.Field<string>("Y") };
            result.ToList().ForEach(x => Console.WriteLine(x.ToString()));
threenewbee 2013-11-01
  • 打赏
  • 举报
回复
string arr = string.Join(", ", list.Select(x => "'" + x + "'")); var sql = "select * from x where y in (" + arr + ")";

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧