繁体   English   中英

SQL 到 LINQ 的转换 C#

[英]SQL to LINQ conversion C#

我正在尝试将此 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 mulitcustomeraccess 
                        WHERE userid = 'Value') )

这是我的 Linq 代码(usr 是WINS_USER表):

billcodelist = String.Join(
    ",", 
    dc.USERLOCATIONs
        .Where(f => f.LOCATIONNAME == usr.LOCATION || 
               dc.MULITCUSTOMERACCESSes
                   .Where(d => d.USERID == usr.Name)
                   .Select(d => d.LOCATIONNAME)
                   .Contains(f.LOCATIONNAME))
        .Select(f => f.BILLCODECUSTNUMLIST)
        .ToArray());

我尝试将我的 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”的定义,并且找不到接受“bool”类型的第一个参数的可访问扩展方法“Select”(您是否缺少 using 指令或程序集引用?)错误。

我的问题是如何将该 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.}
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的记录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM