iT邦幫忙

0

C# Lambda語法

Ks 2020-09-11 22:01:221153 瀏覽
  • 分享至 

  • xImage

我會讀取一個資料表User Table,

資料表欄位有userid,balance,email等等

然後前端網頁有個欄位讓用戶選擇userid, 可以選擇多個值,

然後傳到後端, 比如傳回來的時候是[1,2,5,7,9]

請問該怎麼使用C#lambda或者linq去列出userId是1,2,5,7,9的用戶?

謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

4
暐翰
iT邦大師 1 級 ‧ 2020-09-11 22:23:00
最佳解答

使用 EF LINQ-to-Entities - Where + Contains

舉例

var users = 你的DbContext.User_Table.Where(_=>new[] {1,2,5,7,9}.Contains(_.userid));

系統會幫你轉成 select * from User_Table where userid in (1,2,5,7,9) SQL

Ks iT邦新手 3 級 ‧ 2020-09-11 22:32:26 檢舉

感谢大大的说明和回答

1
舜~
iT邦高手 1 級 ‧ 2020-09-11 22:25:09
  1. 先把"1,2,5,7,9"轉成陣列 (比如說對','做字串分割)
  2. 善用Enumerable.Contains 方法
Ks iT邦新手 3 級 ‧ 2020-09-11 22:32:53 檢舉

感谢大大回答

我要發表回答

立即登入回答