无法编写Linq查询[英] unable to write Linq query

本文是小编为大家收集整理的关于无法编写Linq查询的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个 SQL 查询.我想将其转换为 LINQ 查询.UserLinks 和 UserRoles 是代表 dbase 表的 c# 类.

select distinct UserLinks.LinkID from UserLinks 
   where RoleID in (select RoleID from UserRoles where UserID=1) and UserLinks.Enable='true'

推荐答案

var roleIds = UserRoles.Where(u => u.UserID == 1)
                            .Select(r=>r.RoleID)
                            .ToList();
var result = UserLinks.Where(u => roleIds.Contains(u.RoleID) && u.Enable)
                            .Select(l => l.LinkID)
                            .Distinct()
                            .ToList();

本文地址:https://www.itbaoku.cn/post/1557102.html

问题描述

I have an SQL query. I want to convert it into a LINQ query. UserLinks and UserRoles are c# classes that represent dbase tables.

select distinct UserLinks.LinkID from UserLinks 
   where RoleID in (select RoleID from UserRoles where UserID=1) and UserLinks.Enable='true'

推荐答案

var roleIds = UserRoles.Where(u => u.UserID == 1)
                            .Select(r=>r.RoleID)
                            .ToList();
var result = UserLinks.Where(u => roleIds.Contains(u.RoleID) && u.Enable)
                            .Select(l => l.LinkID)
                            .Distinct()
                            .ToList();