将此SQL脚本转换为linq时遇到一些麻烦。 “fn_Split_t”函数只是拆分逗号分隔值。令我困惑的部分是那里的团体,因为它似乎抛弃了跟随它的一切。
declare @count int
select @count = COUNT(*) from dbo.fn_Split_t(@portfolios)
select
cc.CustodianCommentId
,cc.[Message]
,Symbol = NULL
,cc.DateStamp
,cc.AppUser
,PositionDate = @date
,cc.[Status]
,cc.Category
,cc.[Group]
from dbo.CustodianComment cc
join dbo.CustodianCommentPortfolio ccp on ccp.CustodianCommentId = cc.CustodianCommentId
join dbo.CustodianPortfolio cp on ccp.CustodianPorfolioId = cp.CustodianPortfolioId
join dbo.fn_Split_t(@portfolios) s on s.items = cp.PortfolioCode
where cp.PositionDate = @date
group by cc.CustodianCommentId, cc.[Message], cc.DateStamp, cc.AppUser, cc.[Status], cc.Category, cc.[Group]
having count(cc.CustodianCommentId) = @count
order by cc.DateStamp desc
尝试排名第一的结果如下:
from c in Context.Comments
join pc in Context.PortfolioComments on c.CommentId equals pc.CommentId
join p in Context.Portfolios on pc.PortfolioId equals p.PortfolioId
where portfolios.Contains(p.PortfolioCode)
&& p.PositionDate == EntityFunctions.TruncateTime(date)
group c by c.CommentId into g
where g.Count() > portfolios.Count()
select new
{
CommentId = c.CommentId,
Message = c.Message,
DateStamp = c.DateStamp,
AppUser = c.AppUser,
PositionDate = p.PositionDate,
Status = c.Status,
Category = c.Category,
CategoryId = c.CategoryId,
Group = c.Group
}
但是这不起作用,因为它说匿名对象中的c不在范围内。顺便说一下,投资组合与在SQL脚本中传递给@portfolios的csv字符串相同。我现在有点卡在这一点上,所以任何帮助都将不胜感激!谢谢!
答案 0 :(得分:1)
string[] portfoliosArray = portfolios.Split(',', StringSplitOptions.RemoveEmptyEntries);
from ... in ...
where portoliosArray.Contains(p.PortfolioCode)
select ...
当投资组合代码列表包含空格或其他空格时,您可能需要修剪条目:
string[] portfoliosArray = portfolios.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToArray();
从原始SQL中读取,您可能需要:
where portfoliosArray.All(p => c.PortFolios.PortfolioCode)
或者:
where c.Portfolios.All(p => portfoliosArray.Contains(p.Portfolio.Code))
虽然我对你的要求并不完全确定。