How to Write this SQL with linq to sql - c#

Let's say I have a list with 2 or more customerIds and a list with two or more order dates. I want an SQL query like this from linq to sql
SELECT *
FROM Orders
WHERE (CustomerId = #CustomerId1
AND (OrderDate = #OrderDate1 OR OrderDate = #OrderDate2))
OR
(CustomerId = #CustomerId2
AND (OrderDate = #OrderDate1 OR OrderDate = #OrderDate2))
The list with CustomerIds and order dates is not fixed, so I need to loop through it when building the query.

I found a solution for this by using PredicateBuilder
from http://www.albahari.com/nutshell/predicatebuilder.aspx

Related

Linq Sql Query result is different from the SQL query run in the database

When I run the query in C# code using Linq the result returned is different from the sql query run in sql server
SQL query
SELECT TOP (1000) [Teamid]
,[TeamName]
,[TemplateId]
,[TemplateName]
FROM [MPFT_SendIT].[dbo].[VMTemplate]
where
Teamid=1
Result
SQL Query of the VMTemplate View
SELECT dbo.Team.Id AS Teamid, dbo.Team.TeamName,
dbo.MessageTemplate.Id AS TemplateId,
dbo.MessageTemplate.TemplateName
FROM dbo.Team INNER JOIN
dbo.TemplateLookup ON dbo.Team.Id =
dbo.TemplateLookup.TeamId INNER JOIN
dbo.MessageTemplate ON dbo.TemplateLookup.TemplateId =
dbo.MessageTemplate.Id
where
TeamId= 1
Result
Linq SQL
var teamid = _db.TeamLookups.Where(i => i.UserId == 20).Select(x =>
x.TeamId).ToList(); // teamid return value is 1
ViewBag.messageTemplate = _db.VMTemplates.Where(i =>
teamid.Contains(i.Teamid));
Linq query only returns one record line 1 of the sql query instead of 2 records as expected. Any help on how to solve this issue ?
Why Contains()? You should use equality operator rather
_db.VMTemplates.Where(i => teamid == i.Teamid).ToList();
Per your comment, then your Linq expression should work just fine. Add a ToList() to it
ViewBag.messageTemplate = _db.VMTemplates.Where(i =>
teamid.Contains(i.Teamid)).ToList();

Select number of occurrences of a string in DataTable

I need find out the number of times a persons name string appears in a DataTable.. Im trying to find out who has resolved the most tickets in 24 hours. I've been able to accomplish this using SQL below but I um un familiar with how to do to this using LINQ. The three columns from the SELECT statement are all varchar(MAX types)
SQL CODE
SELECT Assigned_Individual,Data_Output_Type,assigned_group, count(Assigned_Individual)
FROM [DATABASE].[DBO].[TABLENAME]
GROUP BY Assigned_Individual, Data_Output_Type, assigned_group
ORDER BY count(1) desc
This will produce a result that will have an additional column telling me how many times the persons name "Assigned_Individual" has occurred in this table
It's a bit tricky in LINQ, but you can do it using the below code. I hope you like the sql style syntax:
var query = from table in tablename
group by new { table.Assigned_Individual, table.Data_Output_Type, table.assigned_group }
into grp
select new
{
grp.Key.AssignedIndividual,
grp.Key.Data_Output_Type,
grp.Key.assigned_group,
Count = grp.Count()
};

Using sql with index in linq query

I'm using a dbcontext linq query:
var list = context.MyTable.Where(x => x.IsValid).ToList();
The SqlProfiler shows this Sql Query:
SELECT * FROM [MyTable] WHERE IsValid = 1
The problem is that in this table I'm using a lot of sql indexes, and by default it uses the wrong index and query is taking a very long time. I need to add the index I have in the table into the query.
In other words how to get this query from linq?
SELECT * FROM [MyTable] WITH(INDEX(PK_MyIndexName)) WHERE IsValid = 1

Getting distinct rows in LINQ

I need to get the distinct row values from a table using LINQ. The query i used is
var results = (from statename in dsobject.dbo_statetable select statename).Distinct();
It is giving all the rows of the table named "dbo_statetable" but i need only the distinct statename. What should be the query to achieve this?
Its sql equivalent is select distinct statename from dbo_statetable
You need to specify the property:
var results = (from x in dsobject.dbo_statetable select x.Statename).Distinct();
// ^^^^^^^^^^
The variable after from does not specify the column. It is like a table alias in SQL. Your LINQ statement is roughly equivalent to this SQL:
SELECT DISTINCT * FROM dbo_statetable AS statename
dsobject.dbo_statetable.Select(s => s.statename).Distinct()

I need Linq Query for this SQL Query

"select count(salary) from employee where employeeID = 10 group by salary" --- Its a SQL Query.
I need Linq Query which would retrieve me same output..?
Please Help me out i am new to Linq
You should also check :
Full aricle : SQL to LINQ ( Visual Representation )
from e in employee
where e.employeeid=10
group e by e.Salary
into grp
select new
{
Salary = grp.Key,
Count = grp.Count()
};
Your query puzzles me from the functional perspective: You want to count the number of different salaries for one employee?
Anyway, i think something like this would do also work (untested)
db.Employees.Where(e=>e.id == 10).Select(s=>s.salary).Distinct().Count()

Categories