How to sql query include row number in linq query in c#? - c#

I'm trying to do the following sql query with linq but I couldn't understand it and it sounds confused.
Can I do it with linq or lambda?Can you help me write this query with linq?
int pp=0;
int typesquery=0;
string query;
if
{
a is change value.
}
I try:
SELECT TOP PERCENT NAME,SURNAME,DEPARTMENTID,DEPARTMENTNAME,ROW_NUMBER() over(PARTITION BY
NAME,SURNAME ORDER BY NAME,SURNAME) AS INF_NUMBER
FROM PERSON AS S
GROUP BY NAME,SURNAME,PERSONNUM,DEPARTMENTID,DEPARTMENTTYPE
THİS Linq query
var query= from x in PERSON
.OrderBy( x => x.NAME, x=>x.SURNAME)
.SELECT (x=>x.NAME,x=>x.SURNAME,x=>x.DEPARTMENTID,x=>x.DEPARTMENTNAME)
but not Couldn't combine batch way.

Related

Is there any way to make this query faster and build where clause outside of loop?

I have the following code, (and I am completely aware about parameterized queries and SQL Injection):
foreach(var item in items)
{
string query = "select sum(convert(decimal(18,3),tbl.Price)) p, sum(convert(decimal(18,2),tbl.Sale)) s from table1 tbl " +
$"where tbl.ID = {item .ID}";
Execute(query);
//Do stuff with query result
}
The problem is I have a lot of items and I have to execute the query for each of the items because the where clause will be complete in each step. I think if I will be able to make my query out side of my loop, my query will be faster. But I don't know how. Is there any way to do this?
Instead of executing the query for every item. You can add group by to your query and execute only once.
string query = "select tbl.ID, sum(convert(decimal(18,3),tbl.Price)) p, sum(convert(decimal(18,2),tbl.Sale)) s from table1 tbl group by tbl.ID ";
var result = Execute(query);
foreach(var item in items)
{
var row = result.Select(r => r.ID == item.ID).FirstOrDefault();
//Do stuff with query result
}
Do not execute the query for each ID separately. Instead, execute a single query for all Ids using group by to get the p and s values for each id and a parameterized in clause (or better yet, a stored procedure with a table valued parameter).
Here is the IN version of the query:
select Id,
sum(convert(decimal(18,3),tbl.Price)) p,
sum(convert(decimal(18,2),tbl.Sale)) s
from table1 tbl
Where Id IN(<1,2,3,4....>)
group by Id
Replace <1,2,3,4....> with parameters like described in this answer.
Here is the table valued parameter version of the query:
select tbl.Id,
sum(convert(decimal(18,3),tbl.Price)) p,
sum(convert(decimal(18,2),tbl.Sale)) s
from table1 tbl
inner join #items i on tbl.Id = i.Id
group by tbl.Id
For a detailed explanation about using table valued parameters, read this answer.

SQL to LINQ Convertion error: DISTINCT an Agregate count cannot be converted to LINQ

I have a SQL query and I am trying to convert it to LINQ. But when I do so, I am getting this error:
SQL cannot be converted to LINQ: DISTINCT an Agregate count cannot be converted to LINQ
SELECT
ST_CN_NO,
DN_DT,
count(ST_CN_NO) as totlines,
count(distinct LOC_CODE) as totloc,
sum(SN_QTY)as totscnqty,
sum(SAP_QTY) as totmpqty
from physical_count
where 1=1
group by ST_CN_NO,DN_DT
order by physical_count.ST_CN_NO
So, how can I convert count(distinct LOC_CODE) to LINQ? Is there any way to do so?
This is my LINQ query whithout adding count(distinct LOC_CODE):
from t in db.PHYSICAL_COUNT
where
1 == 1
group t by new {
t.ST_CN_NO,
t.DN_DT
} into g
orderby
g.Key.ST_CN_NO
select new {
ST_CN_NO = (Int32?)g.Key.ST_CN_NO,
g.Key.DN_DT,
totlines = (Int64?)g.Count(),
totscnqty = (Int32?)g.Sum(p => p.SN_QTY),
totmpqty = (Int32?)g.Sum(p => p.SAP_QTY)
}

Select row with max value after group in Linq Nhibernate

I have a table:
Table { Id, Date, Number, Bool }
I need to group it by Number, select the row with max Date inside each group, and retrieve Id for each group. In the end I need to filter that to only have records that are !Bool. I am trying to do this with Linq Nhibernate.
This SQL seems to be doing what I want:
select Id from
(select MAX(Date) as Dt, Number as N from Table group by Number) t, Table table
where table.Date = t.Dt and table.Number = t.N and table.Bool = 0
but turns out NHibernate does not allow for subqueries to be in from. How do I write this with Linq Nhibernate?
It's also quite important for it to be efficient, so I would rather avoid having subqueries in select or where if they iterate over the whole set and (N+1) query problem.
The straightforward approach doesn't work either:
Session.Query<Table>().GroupBy(x => x.Number)
.Select(x => x.Where(y => y.Date == x.Max(z => z.Date)))...

EF (LINQ to SQL) - How to query?

I have the concept of a document that has keyword/s. EF abstracted out the document-keyword joining table to an association.
The structure looks like this
Document: ID (PK)
Document_Keyword: DocumentID (PK), Keyword (PK)
Keyword: Keyword (PK)
I have the requirement to return a list of documents where they contain ALL keywords in a string[]
If I was doing this in SQL it would be similar to below
with t as (
select 'keyword1' KEYWORD union
select 'keyword2'
)
select DocumentID,count(*) from [dbo].[Document_Keyword] p
inner join t on p.KEYWORD = t.KEYWORD
group by DocumentID
having count(*) = (select count(*) from t)
Im struggling to form a linq query that will give me the same result.
I have tried the following LINQ statement however it does returns documents that contain 1 or more of the keywords in the array. I require that documents are only returned if ALL keywords match.
var query = (from k in db.KEYWORD
from b in k.DOCUMENT
join q in arrKeywords //array of string[]
on k.KEYWORD equals q
select new Document()
{
Filename = b.FILENAME,
Description = b.TITLE
});
Any ideas?
Cheers
Jeremy
If I get you well you want entries of which all keywords match exactly, i.e. it doesn't have any other keywords. A way too achieve this is
var kwc = arrKeywords.Count();
var query = from k in db.KEYWORD
let kw = k.DOCUMENT.Select(d => d.KEYWORD)
where kw.All(kw1 => arrKeywords.Contains(kw1))
&& kw.Count() == kwc;
The generated query is still much longer than a hand-coded one would be, but I think the database's query optimizer should be able to handle this.

Subsonic 3 query - aggregate error

I'm creating a simple query with aggregates. The example is:
string query = new SubSonic.Query.Select(
SubSonic.Query.Aggregate.GroupBy("ProductID", "ID"),
SubSonic.Query.Aggregate.Max("Price", "MaxPrice")
).From("Orders").ToString();
The Sql Result is:
SELECT ProductID AS ID, MAX(Price) AS MaxPrice
FROM [Orders]
when the result should be:
SELECT ProductID AS ID, MAX(Price) AS MaxPrice
FROM [Orders]
GROUP BY ProductID
In SubSonic2.2 the result is correct but in Subsonic3, the GROUP BY statement dissapears and the query return only one row.
Is my SqlQuery expression correct or is it a bug in SubSonic3?
Not entirely sure about the exact syntax, I use the
MyTableClass.All().Max
Notation for querying it.Your example looks wrong though, because your groupBy is inside your select. The groupBy is not part of the select, so maybe you need to move that GroupBy outside your select

Categories

Resources