views:

64

answers:

2

How do I convert this SQL query to LINQ-to-SQL?:

select 
    COUNT(ua.UserAlertID) as Alerts, 
    ph.LastName +' '+ ph.MiddleName as Name,
    ph.Email,us.UserSystemID,
    ur.UserStatus from PHUser ph 
inner join UserSystem us on us.UserID=ph.UserID
inner join UserRole  ur on ur.UserID=ph.UserID
inner join Role rr on rr.RoleID=ur.RoleID
inner join UserAlerts ua on ua.SeniorID=ph.UserID
group by ph.LastName,ph.MiddleName,ph.Email,us.UserSystemID,ur.UserStatus 

I have converted most of the above query to LINQ but I got stuck to counting the number of values on the ua.UserAlertID column: COUNT(ua.UserAlertID) as Alerts.

How do I convert that to LINQ?

Kindly suggest How to convert COUNT(ua.UserAlertID) as Alerts in Linq??

Thanks

A: 

A number of people can help you with this query. It's pretty straightforward. There is a product (very inexpensive) that can help you with these sorts of things. The product is Linqer (http://www.sqltolinq.com/). This tool will convert most SQL statements to Linq. There is a 30 day trial period and I think the cost is < $40. I have used it many times to get up to speed on converting SQL queries to Linq.

Then I might suggest you get a free product called LinqPad. This will allow you to prototype your Linq queries before pasting them into production code. It is a phenomenal tool.

Randy Minder
"A number of people can help you with this query. It's pretty straightforward" - so why don't you help out instead of recommending chargable tools. Not a useful answer.
Kev
Linqer is free for 30 days. He can get his query converted at no charge. Besides, what does he learn if I create the query for him?
Randy Minder
I need only help in converting COUNT(ua.UserAlertID) as Alerts in Linq??
Tarun
@Tarun - Do you want the total number of rows in the UserAlerts table or the total number of rows that match the join?
Randy Minder
I want the count of alerts as per the user. In my UserAlerts tables, there are three alerts for user1 and one alert for user2. So I need 3 as count for user1 and 2 as count for user2.
Tarun
Want the total rows that match the join
Tarun
A: 

Can I see your converted LinQ?

If your Query is ok, you got a line like

group s by new {ua.UserAlertId,...,...} into temp.

as I remember in select new {} part use

select new 
{
Total Alerts = temp.Key.UserAlertdId.Count(), ...,...,...,...
}

can you please try this and notify me?

Serkan Hekimoglu
@Serkan. I have tried the above but didnt get Count() after temp.Key.UserAlertId.
Tarun
var p=(from ph in base.DataContext.User join us in base.DataContext.USyt on ph.UserID equals us.UserID join ur in base.DataContext.URle on ph.UserID equals ur.UserID join rr in base.DataContext.Rol on ur.RoleID equals rr.RoleID join ua in base.DataContext.UserAlerts on ph.UserID equals ua.SeniorID group new{ph,us,ur,rr,ua} by new{ ph.LName,ph.MName,ph.Email,us.UserSystemID,ur.UserStatus,ua.UserAlertID,ph.UserID} into g select new{Name=g.Key.MiddleName+" "+g.Key.LastName,Email=g.Key.Email,SystemID=g.Key.UserSystemID,Status=g.Key.UserStatus,g.Key.UserID,count=g.Key.UserAlertID}).Distinct();
Tarun