2

我有我认为是复杂的 SQL 查询,我需要将其转换为 LINQ 查询。但是,我只是 LINQ 的初学者,不知道如何让它工作。我试过搜索互联网,但仍然无法正常工作。

这是我的 SQL 查询:

SELECT
    a.empid,
    CAST(IF(COUNT(*)  > 10,FLOOR(COUNT(*) / 10),1) AS CHAR(100)) lim,
    CAST(GROUP_CONCAT(internalid) AS CHAR) internalIDS
FROM tblLogs a
INNER JOIN
    (
        SELECT 
            DISTINCT empid 
        FROM tblLogs
        WHERE IsDeleted = 0 AND DateAdded = 2013-04-18
    ) b ON  a.empid = b.empid
WHERE IsDeleted = 0 AND Remarks NOT LIKE '%proxy date used%' AND DateAdded = 2013-04-18 AND RecType = 8
GROUP BY empid;

你好。这是我更新的 linq 查询,但它返回错误 Linq to Entities 无法识别方法 string.join。这有什么问题?谢谢。:)

var rows = from rec in context.tblWMSLogs join rec1
                        in context.tblWMSLogs.Where(t => t.DateAdded == refDate2 && t.IsDeleted == 0)
                        on rec.EmpID equals rec1.EmpID
                    where rec.DateAdded == refDate2 && rec.IsDeleted == 0 && !rec.Remarks.Contains("proxy date used") && rec.RecType == recType
                    group rec by rec.EmpID into g
                    select new WMSRandomViewModel
                    {
                        EmpID = g.Key,
                        Lim = (g.Count() > 10 ? Math.Ceiling(g.Count() / 10d) : 1),
                        InternalIDs = string.Join(",", g.OrderBy(s => s.InternalID).Select(s => s.InternalID))
                    };//string.Join(",", g.OrderBy(s => s.InternalID).Select(s => s.InternalID))
            //return rows.ToList();
            return rows.ToList();
4

1 回答 1

1

这是你想要的吗?

var rows =
        from rec in context.tblLogs.AsEnumerable()
        join rec1 
            in context.Where(t => t.DateAdded == refDate && t.IsDeleted == 0)
            on rec.EmpID equals rec1.EmpID
        where rec.DateAdded == refDate && rec.IsDeleted == 0 && !rec.Remarks.Contains("proxy date used") && rec.RecType == recType
        group rec by rec.EmpID into g
        select new
        {
            g.Key,
            lim = (g.Count() > 10 ? Math.Floor(g.Count() / 10d) : 1).ToString(),
            InternalIDS = string.Join("", g.OrderBy(s => s.InternalId).Select(s => s.InternalId))
        };

现在,我的问题,

为什么你的 sql 中有一个内部连接?内连接中的where子句是否可以移动到主查询的where子句中

于 2013-04-22T04:56:10.870 回答