我有以下SQL查询
;with cte as( select a.* from [dbo].[AccountViewModel] a where a.COLLECTORID = 724852 and a.MONTH = 12 and a.YEAR=2015) select * from cte c where c.DispCode in ('Deceased','DND','WN','WI','NC','NORESPONSE','SKIP','SHIFTED','SFU') OR (c.DispCode in('PTP','DIB','WCE','DP') and convert(varchar(11), c.PTPDate) >=convert(varchar(11), getdate())) OR (MONTH(c.LastPaymentDate) = 12 and YEAR(c.LastPaymentDate)=2015)
我需要将其转换为等效的Linq查询(C#).
Cte部分正常运行以下程序(我已经交叉检查了记录)
private ListGetAllAcountsForLoggedInAgents() { var allAcountsForLoggedInAgents = new List (); allAcountsForLoggedInAgents = new ViewModelDatabase() .Accounts .Where(a => a.COLLECTORID == 724852 && a.MONTH == DateTime.Now.Month && a.YEAR == DateTime.Now.Year ) .ToList(); return allAcountsForLoggedInAgents; }
但CTE以外的部分工作不正常(表示记录不正确)
GetAllAcountsForLoggedInAgents() .Where ( a => ("Deceased,DND,WN,WI,NC,NORESPONSE,SKIP,SHIFTED,SFU".Split(',').Any(x => x.Contains(a.DispCode))) || ("PTP,DIB,WCE,DP".Split(',').Any(b => b.Contains(a.DispCode)) && a.PTPDate >= DateTime.Now) || (a.LastPaymentDate.Value.Month == 12 && a.LastPaymentDate.Value.Year == 2015) )
我相信可能是我以错误的方式使用"任何".
任何人都可以帮忙吗?
谢谢
这个条件与该IN
条款不同
("Deceased,DND,WN,WI,NC,NORESPONSE,SKIP,SHIFTED,SFU".Split(',').Any(x => x.Contains(a.DispCode)))
因为它搜索a.DispCode
其中一个字符串.你应该使用相等:
("Deceased,DND,WN,WI,NC,NORESPONSE,SKIP,SHIFTED,SFU".Split(',').Any(x => x == a.DispCode))
这并不理想,因为Split
操作不是免费的,所以您不希望将其作为查询的一部分.制作一个static
字符串数组:
static readonly string[] DispCodeFilter = new string[] { "Deceased", "DND", "WN", "WI", "NC", "NORESPONSE", "SKIP", "SHIFTED", "SFU" }; ... (DispCodeFilter.Any(x => x == a.DispCode))