Solved

convert sql query to linq query

Posted on 2011-03-16
5
605 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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