SQL到LINQ转换C#
我正在尝试将此sql语句转换为linq,需要一些帮助:SQL到LINQ转换C#,c#,sql,linq,C#,Sql,Linq,我正在尝试将此sql语句转换为linq,需要一些帮助: SELECT * FROM userlocation ul INNER JOIN wins_user w ON ul.locationname = w.location WHERE ul.locationname = 'Value' OR ( NOT EXISTS(SELECT * FROM mulitcustomerac
SELECT *
FROM userlocation ul
INNER JOIN wins_user w
ON ul.locationname = w.location
WHERE ul.locationname = 'Value'
OR ( NOT EXISTS(SELECT *
FROM mulitcustomeraccess
WHERE userid = 'Value') )
这是我的Linq代码(usr是WINS\u用户
表):
我尝试将我的linq代码更新为
billcodelist = String.Join(
",",
dc.USERLOCATIONs
.Where(f => f.LOCATIONNAME == usr.LOCATION ||
!dc.MULITCUSTOMERACCESSes
.Any(d => d.USERID == usr.Name)
.Select(d => d.LOCATIONNAME)
.Contains(f.LOCATIONNAME))
.Select(f => f.BILLCODECUSTNUMLIST)
.ToArray());
但是我得到了以下错误:
“bool”不包含“Select”的定义,并且不可访问
扩展方法“Select”接受类型为“bool”的第一个参数
无法找到(是否缺少using指令或程序集
参考?)错误
我的问题是如何将SQL转换为linq,我的错误之处是什么
var results = USERLOCATION.Join(db.WINS_USER, w => w.LOCATION, ul => ul.locationname, (w, ul) => new {w, ul})
.Where(_ => _.ul.LOCATIONNAME == 'Value' || !db.MULITCUSTOMERACCESS.Any(m => m.USERID == 'Value'))
.Select(_ => _.ul.BILLCODECUSTNUMLIST);
var billCodeList = string.Join(",", results);
Where子句总是需要布尔表达式,您正在将Where传递到Where,但Where不会返回布尔值,而是返回IQueryable。在上面的例子中,我使用了
Any
来评估MULITCUSTOMERACCESS
是否在您使用where
的地方有一个记录,这里是另一个选择
var result = from ul in UserLocation
join winUser in Wins_User on ul.locationName equals winUser.Location
where ul.locationName == 'value'
|| !MultiCustomerAccess.Any(x=> x.userId == "value")
select new { // your projections.}
仅供参考,您不需要
ToArray
,因为字符串过载。Join
接受IEnumerable
。Any
返回bool(true
,如果有任何项满足条件),但是您尝试调用。在结果上选择,这就是您得到该错误的原因。如果.Any
应该是.Where
子句的一部分,那么在.Any
之后需要另一个右括号。您也不会显示在哪里声明了usr
。您不想像在SQL中一样在linq中加入一个连接吗?
var result = from ul in UserLocation
join winUser in Wins_User on ul.locationName equals winUser.Location
where ul.locationName == 'value'
|| !MultiCustomerAccess.Any(x=> x.userId == "value")
select new { // your projections.}