Solved

convert sql query to linq query

Posted on 2011-03-16
5
598 Views
Last Modified: 2013-11-11
Hi,

Can someone please convert the following sql query to linq please?

it should list unique records and how many records there are with that text
SELECT distinct columnname as var_text, COUNT(columnname) as var_count FROM tbl_data where columnname > '' GROUP BY columnname

Open in new window

0
Comment
Question by:OTM
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35147631
Here's my interpretation:
var results = from item in (from columnname in tbl_data
                            where columnname > ''    // Not sure why greater than empty string, but copied nonetheless
                            select columnname).Distinct()
              group item by item into g
              select new { var_text = g.Key, var_count = g.Count() };

Open in new window

0
 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 35150758
kaufmed, I think your solution should be corrected as follows

var results = from item in (from tbl in tbl_data
                        where tbl.columnname.Trim().Length > 0
                        select tbl.columnname).Distinct()
group item by item into g
select new { var_text = g.Key, var_count = g.Count() };

Open in new window



-Kusala
0
 

Author Comment

by:OTM
ID: 35150971
thanks kusala, that lists the distinct entries correctly but lists the count for each one as 1. My aim is for the var_count field to be the total no. of records with the columnname text being var_text.

i hope this makes sense, thanks again.
0
 
LVL 11

Accepted Solution

by:
Kusala Wijayasena earned 500 total points
ID: 35151064
OK, if your requirement is that, change the query as follows

var results = from tbl in tbl_data
              where tbl.columnname.Trim().Length > 0
              group tbl by tbl.columnname into g
              select new { var_text = g.Key, var_count = g.Count() };

Open in new window


-Kusala
0
 

Author Closing Comment

by:OTM
ID: 35152237
kusala, thats great. It does exactly what i needed :-D
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now