2

I would like to know how can i change the following sql statement into Linq or Lambda Extension in C#

SELECT m.mdes as AgeGroup,COUNT(DISTINCT(mbcd))as "No.of Member" FROM mageg m
LEFT JOIN  (select distinct(mbcd) ,mage
FROMtevtl
JOIN mvipm 
ON tevtl.mbcd = mvipm.mvip
WHERE datm >= '2014-04-01'
AND datm <= '2014-04-30'
)vip
ON m.tage >= vip.mage AND m.fage <= vip.mage
GROUP BY m.mdes

I manage to do the first half of the LINQ statement. Not sure If it is correct here is the first half. I do not know how to connect with the left join.

(from mem in mvipms
from log in tevtls
from grp in magegs
where mem.mage >=grp.fage && mem.mage <=grp.tage && mem.mvip.Equals(log.mbcd)
&& log.datm >= DateTime.Parse("2014-04-01") && log.datm <= DateTime.Parse("2014-04-30")
select new {mem.mvip,grp.mdes}).Distinct()

Pls advice. I am using the MSSQL 2008 and VS2010.

Thanks a million.

2
  • Entity framework? Commented Jul 4, 2017 at 8:01
  • I suggest with such a question to change original table & attribute names to something more self explanatory. Also the question could be improved by adding description of the query type like grouped left join with interval condition or similar. Of course sample data would help too. Commented Jul 4, 2017 at 10:59

1 Answer 1

1

I am no expert on LINQ, but since no-one else has answered, here goes!

Firstly you cannot (AFAIK) do a LINQ join on anything other than equals so the structure of your LEFT JOIN has to change. Partly for debugging purposes, but also for readability, I prefer to layout my LINQ in bite-size chunks, so what I would do in your case is something like this (assuming I have understood your data structure correctly):

    var vip = (from t in tevtl
               join v in mvipl
               on t.mbcd equals v.mvip
               where t.datm >= new DateTime(2014, 4, 1) && t.datm <= new DateTime(2014, 4, 30)
               select new { t.mbcd, v.mage }).Distinct();
    var mv = from m in magegl
             from v in vip
             where m.tage >= v.mage && m.fage <= v.mage
             select new
             {
                 m.mdes,
                 v.mbcd
             };
    var gmv = from m in mv
              group m by new { m.mdes } into grp
              select new
              {
                  mdes = grp.Key.mdes,
                  CountMBCD = grp.Count()
              };
    var lj = from m in magegl
             join v in gmv
             on m.mdes equals v.mdes into lhs
             from x in lhs.DefaultIfEmpty()
             select new
             {
                 AgeGroup = m.mdes,
                 CountMBCD = x != null ? x.CountMBCD : 0
             };

By way of explanation. mv is the equivalent structure for your left join in that it has the relevant where clause, but obviously it does not contain any nulls - it is equivalent to an INNER JOIN. gmv is a group on mv, so is still effectively an INNER JOIN. Then to pick up the missing mdes (if any) I re-join on magel with the added syntax DefaultIfEmpty() - this converts the join into the equivalent of a LEFT OUTER JOIN.

Without any sample data, I haven't been able to test this, but I hope it gives you enough to point you in the right direction!

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

2 Comments

Your mv query is inner join, he needs the left join.
@VojtěchDohnal Yes I know. I point this out in my answer, plus how to add back any potential missing answers. The resultset in lj is the desired LEFT JOIN

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.