molu2008
molu2008

Reputation: 1257

How to convert normal sql statement to linq to sql

Hey i got really simple question: o Have to count how many one e-mail address occurs for particular user.

I have working sql code:

 SELECT
    name, email, COUNT(*)
FROM
    users
GROUP BY
    name, email
HAVING 
    COUNT(*) > 1

But i have some troubles to translate it to Linq-to-sql Could anybody help me.

Upvotes: 2

Views: 40

Answers (2)

nitish
nitish

Reputation: 81

Try this:-

    var _List = entities.Employee_Test.GroupBy(n =>new 
                                                 {
                                                   n.Employee_Salary,
                                                   n.Employee_name 
                                                 })
                   .Select(n => new
                                {
                                Employee_name= n.Key.Employee_name,
                                Employee_Salary = n.Key.Employee_Salary,
                                count= n.Count()
                                })
                   .Where(n => n.count > 1).ToList();

Upvotes: 0

Gonzalo
Gonzalo

Reputation: 2876

from u in users
group u by new {
  u.name,
  u.email
} into g
where g.Count() > 1
select new {
  g.Key.Email,
  g.Key.Nombre,
  userCount = (Int64?)g.Count()
}

Also you can use Linqer to learn more about complex Linq queries

Linqer is a SQL to LINQ conversion tool, it isn't free but has a trial version.

You may also try LINQPad. Is a great tool and the standard edition is free

Upvotes: 1

Related Questions