Solved

convert sql query to linq query

Posted on 2011-03-16
5
594 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 74

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

13 Experts available now in Live!

Get 1:1 Help Now