2

I have what I think is a complex SQL query, and i need to convert it to LINQ query. But, I'm only a beginner in LINQ and don't know how to get this working. I've tried searching the internet and still unable to get it to work.

Here is my SQL query:

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;

Hi. Here is my updated linq query, but it returns an error Linq to Entities does not recognize the method string.join. What's wrong with this? Thanks. :)

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();
1
  • Does your attempt compile? If so, does it give you what you want? What exactly is your question? Commented Apr 22, 2013 at 3:25

1 Answer 1

1

is this like what you want?

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))
        };

now, my question,

why is there a inner join in your sql? could the where clause in the inner join be moved to the where clause of the main query

Sign up to request clarification or add additional context in comments.

5 Comments

hi. there is an inner join because the data can come from different tables.
ah~ ok, so the tblLogs table could be a different table?
Hi. Thanks. It's now showing any errors now but i still am not able to get the query results. I'm using mysql as my database. Maybe it has some issues on the DateTime part of the where clause.
you can try to remove rec.DateAdded == refDate, and check if you get results
are you using something which translate linq in to MySql script? if this is the case you can't use string.Join if the library you use does not support the method. so what you are actually doing with the linq? what library you are using? have you research on how the library implements GROUP_CONCAT?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.