0

My SQL Server query (below) works perfectly fine, and I'm trying to convert it to LinQ in C#.

SQL Query:

SELECT addressline3, city, COUNT(*) as 'InstitutionNumber'
FROM institutionenquiries
WHERE CITY = 'AHMEDABAD'
GROUP BY addressline3, city
ORDER BY city;

Desired output is:

enter image description here

I'm able to draw the LinQ query in below format, which gives me correct output
(except for COUNT(*) as 'InstitutionNumber').

LINQ Query:

var obj = (from u in dbContext.InstitutionEnquiry
           where u.City == data.Trim().ToLower()
           select new {
               AddressLine3 = u.AddressLine3.Trim().ToLower(),
               City = u.City.Trim().ToLower(),
               InstitutionNumber = (from a in dbContext.InstitutionEnquiry
                                    where a.City == data.Trim().ToLower()
                                    select a).Count()
           }).ToList();

This gives me count for 'InstitutionNumber' = 3 for all.

I'm not able to get the count aspect correctly. I've referred to this post's answer and it gives me only count, but I'm not able to nest it within my LinQ query.
Any helpful inputs will be appreciated.

1
  • Why are you converting 'data' to lowercase (u.City == data.Trim().ToLower()) when the 'u' from database is in uppercase? Commented Jan 21, 2017 at 15:52

3 Answers 3

1

The first observation is that since SQL has a GROUP BY, LINQ should have a GroupBy, too.

The second observation is that since non-aggregate columns in SQL are group by columns, the results you select in LINQ would come from the group's key.

Now we can write the query:

var res = dbContext.InstitutionEnquiry
.Where(u => u.City == data.Trim().ToLower())
.GroupBy(u => new {
    AddressLine3 = u.AddressLine3.Trim().ToLower(),
    City = u.City.Trim().ToLower()
 }).Select(g => new {
     g.Key.AddressLine3,
     g.Key.City,
     Count = g.Count()
}).ToList();
Sign up to request clarification or add additional context in comments.

Comments

1

I think you can use this LinQ :

var obj = dbContext.InstitutionEnquiry
        // WHERE CITY = 'AHMEDABAD'
        .Where(w => w.City == "AHMEDABAD")  // => Note           
        // GROUP BY addressline3, city
        .GroupBy(g => new {g.AddressLine3, g.City})
        // SELECT addressline3, city, COUNT(*) as 'InstitutionNumber'
        .Select(c => new {c.Key.AddressLine3, c.Key.City, InstitutionNumber = c.Count()})
        // ORDER BY city
        .OrderBy(o=> o.City)
        .ToList();

Note: For ignoring case sensitivity You can use :

.Where(string.Equals(w.City.Trim(), data.Trim(), StringComparison.CurrentCultureIgnoreCase))

Comments

1

LINQ :

var obj = (from p in dbContext.InstitutionEnquiry
group p by new
{
    p.addressline3,
    p.city
} into grp
select new
{
    grp.Key.addressline3,
    grp.Key.city,
    InstitutionNumber = grp.Count(),
}).ToList();

Equivalent Lambda expression :

var obj = dbContext.InstitutionEnquiry
.GroupBy(p => new { p.addressline3, p.city})
.Select(p => new { p.Key.addressline3, p.Key.city, InstitutionNumber = p.Count() })
.ToList();

Comments

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.