SELECT [UserId] FROM [TU_LMS].[dbo].[MstUser] where RoleId in (3,5)
Loading
SELECT [UserId] FROM [TU_LMS].[dbo].[MstUser] where RoleId in (3,5)
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 17, 2023, 11:14 AM EDT
You can use below:
var userIds = dbContext.MstUser .Where(u => u.RoleId == 3 || u.RoleId == 5) .Select(u => u.UserId);Thanks
Uday DodiyaRank #239 · 10.3K reputationPosted Mar 18, 2023, 12:53 AM EDT
Hi,
you can write linq query as per below
Option 1
var roles = new int[] { 3, 5 }; var result = dbContext.MstUser .Where(u => roles.Contains(u.RoleId)) .Select(u => u.UserId);Option 2
var result = dbContext.MstUser .Where(u => u.RoleId == 3 || u.RoleId == 5) .Select(u => u.UserId);Raj BhattRank #513 · 4.8K reputationPosted Mar 18, 2023, 12:57 AM EDT
from user in TU_LMS.dbo.MstUser where user.RoleId == 3 || user.RoleId == 5 select user.UserIdManikandan MurugesanRank #300 · 8.4K reputationPosted Mar 18, 2023, 1:33 AM EDT
you can write the equivalent LINQ query for the above SQL query as follows:
var result = from user in db.MstUser where user.RoleId == 3 || user.RoleId == 5 select user.UserId;Here,
dbis an instance of the DataContext class representing your database context.Alternatively, you can use the lambda syntax for the same query:
var result = db.MstUser .Where(user => user.RoleId == 3 || user.RoleId == 5) .Select(user => user.UserId);This will give you a collection of
UserIds that match the condition specified in the SQL query.Rajeesh MenothRank #118 · 18.4K reputationPosted Mar 18, 2023, 3:48 AM EDT
Hi,
Try this:
var userIds = from user in dbContext.MstUser where new int[] {3, 5 }.Contains(user.RoleId) select user.UserId;Tuhin PaulRank #7 · 71.9K reputationPosted Mar 18, 2023, 8:57 AM EDT
If you only need the UserId column, you can modify the Select() clause to only select that column. This can reduce the amount of data that needs to be retrieved from the database, which can improve performance.
var roleIds = new List { 3, 5 }; var userIds = dbContext.MstUser.AsQueryable() .Where(u => roleIds.Contains(u.RoleId)) .Select(u => u.UserId);Tuhin PaulRank #7 · 71.9K reputationPosted Mar 19, 2023, 8:53 PM EDT
SELECT [UserId] FROM [MstUser] WHERE [RoleId] IN (3, 5)LINQ code filters the MstUser table by checking if the RoleId column is contained in the roleIds list and then selects the UserId column. The equivalent SQL query uses the IN operator to filter the MstUser table by checking if the RoleId column is in the list of values (3, 5) and selects the UserId column.