3

How can I convert the following SQL queries into LINQ query form in C#, .NET 3.5 code: 1)

select COUNT(distinct Skill_Name) 
from Table1  
where Department = 'ABC' and  Skill_Name is not null 

2)

select distinct location, country from Customer where  Customer_Code ='1001';
2
  • See my asnwer's edit for second query Commented Dec 28, 2011 at 13:03
  • 1
    @sukmar for respect accept answers Commented Dec 29, 2011 at 10:21

3 Answers 3

3

I suspect you want:

var query = from entry in dbContext.Table1
            where entry.Department == "ABC" && entry.SkillName != null
            select entry.SkillName;

var count = query.Distinct().Count();

Or using extension method syntax, in one go:

var count = dbContext.Table1
                     .Where(entry => entry.Department == "ABC" && 
                                     entry.SkillName != null)
                     .Select(entry => entry.SkillName)
                     .Distinct()
                     .Count();

As shown by mesiesta, you can combine query expressions with calls not supported within query expressions, but I tend to assign the query expression to an intermediate variable... I personally find it clearer, but use whichever you (and your team) prefer.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I want to add as columns in an exisitng datatable these unique skillNames which resulted from the above query. How??
@sukumar: If you just want the distinct names, get rid of the Count() call at the end. You can count the number of results when you've fetched them down to the datatable.
Hi, Assume the above query results count as 2 and next I want to add these two skillNames as columns in a existing datatable . How??
@sukumar: As I've said, you wouldn't use the Count() call there. I don't know about transferring into an existing DataTable, but you can use the LINQ to DataSet extension methods to create a new DataTable from a query result. See DataTableExtensions.CopyToDataTable
3

Something like this

 int count = (from p in Table1
              where p.Department == "ABC" && p.Skill_Name != null
              select p.Skill_Name).Distinct().Count(); 

For second query you can use this

 var query= (from p in Customer
             where p.Customer_Code=="1001"
             select new { Location=p.location ,Country=p.country}).Distinct();

3 Comments

Hi, Assume the above query results count as 2 and next I want to add these two skillNames as columns in a exisitng datatable . How??
Hi, I don't understand what you mean. Can you explain it more detailed?
Ok, I get what you want. I don't hear anything about it, but I guess, you can't add columns to your table via LINQ. You can update your table's rows, but adding collumns seems like a not work of LINQ.
2

you can use linqpad to convert to linq and lambda expressions

2 Comments

Hi, Pls help me how to get a linQ query for this SQL Statement: select distinct location, country from Customer where Customer_Code ='1001';
var query = (from q in customer where q.customer_code == "1001" select new { Location = q.location, Country = q.country }).Distinct();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.