I have written this SQL statement:
select id,ProductAbbr
from Product
where id in (Select max(id) from Product group by ProductAbbr)
My linq query is :
var prod = (from t in _context.Product
group t by t.ProductAbbr
into g
select new
{
Id = (from t2 in g select t2.Id).Max()
}).ToList();
// int idlist = Int32.Parse(prod);
var request = (from p in _context.Product
where(p => prod.Contains(p.Id))
select new
{
p.Id, p.ProductAbbr
}).ToList().Distinct();
I get this error
CS0136 C# A local or parameter named 'p' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
Isn't it just
var prod = (from t in _context.Product
group t by t.ProductAbbr
into g
select new
{
Id = (from t2 in g select t2.Id).Max()
}).ToList();
//int idlist = Int32.Parse(prod);
var request = (from p in _context.Product
join p1 in prod on p1.Id equals p.Id
select new
{
p.Id,
p.ProductAbbr
}).ToList().Distinct();
?
Note that I have changed
where(p=> prod.Contains(p.Id))
To
join p1 in prod on p1.Id equals p.Id
Note that this will make your query syntaxically valid, but it seems to also be possible to rework it in many other ways according to what I see.
The second query doesn't seem to bring anything useful, at least in your example.
Related
I have the following SQL statement:
SELECT b.Brand_key, b.BrandName, Sum(po.Quantity) AS Quantity
FROM Brands b
INNER JOIN Products p ON b.Brand_key = p.Brand_fkey
INNER JOIN ProductOrders po ON p.Product_key = po.Product_fkey
GROUP BY b.Brand_key
ORDER BY Quantity DESC, b.BrandName ASC
which works perfectly. Distinct values are returned as expected. I've attempted to turn it into Linq, but I feel like I'm going down a rabbit hole that never ends.
var testModelTwo =
(from b in db.Brands
join p in db.Products on b.brandKey equals p.brandKey
select new { brandKey = b.brandKey, BrandName = b.BrandName, ProductKey = p.productKey }
into groupResultBP
join po in pom on groupResultBP.ProductKey equals po.Product_fkey
select new
{
brandKey = groupResultBP.brandKey,
BrandName = groupResultBP.BrandName,
Quantity = po.Quantity
} into gr
group gr by gr.brandKey into gd
select new { Brand = gd.Key, BrandCounter = gd.Sum(c => c.Quantity) } into sumGroup
select sumGroup).ToList();
This seems to get to the point where I have a key and the sum, but need to do another join to get back the brand item information with sumGroup.
I feel like I've gone round the houses about 100 times and this is much easier than I'm making it! Is what I've done so far correct?
I would like to translate the following SQL into LINQ:
select count(p.ID) as NumPosts,
count(t.Trustee_ID)as TrusteePost,
count(pat.ID)as PatientPost,
count(s.ID) as SpecialistPost
from [dbo].[Posts] as p
left join [dbo].[Trusteehips] as t
on p.Autor_ID = t.Trustee_ID
left join [dbo].[Patients] as pat
on p.Autor_ID = pat.ID
left join [dbo].[Specialists] as s
on p.Autor_ID = s.ID
where p.Deleted = 0
I I've tried this:
var res = from p in context.Posts
join t in context.Trusteeships
on p.Autor.ID equals t.Trustee.ID into tGroup
join pat in context.Patients
on p.Autor.ID equals pat.ID into patGroup
join s in context.Specialists
on p.Autor.ID equals s.ID into sGroup
select new NumUserPosts
{
//CountAllPosts = ?
TrusteePost = tGroup.Count(),
PatientPost = patGroup.Count(),
SpecialistPost = sGroup.Count()
};
But result is this:
1 0 0
0 0 1
0 0 1
0 1 0
and etc.
I expect result
TrusteePost PatientPost SpecialistPost
1000 2000 3000
Why when i try to count group return this result?
SQL query is correct. I would like to translate into LINQ.
The query returns 0 or 1 records per joined Trustee, etc. because you outer join by the unique primary key. So a join into (which is a GroupJoin in fluent syntax) produces a group of 0 or 1 records. If you run the generated SQL query and view the raw query result you'd probably understand better what's going on.
The problem is, there is no LINQ equivalent for count(t.Trustee_ID), etc. Therefore it's impossible to do what you want in one query without "hacking".
Hacking it into one query could be done like so:
(from p in context.Posts.Take(1)
select new
{
TrusteePost = context.Posts
.Count(p1 => context.Trusteeships.Any(x => x.ID == p1.Autor.ID)),
PatientPost = context.Posts
.Count(p2 => context.Patients.Any(x => x.ID == p2.Autor.ID)),
SpecialistPost = context.Posts
.Count(p3 => context.Specialists.Any(x => x.ID == p3.Autor.ID))
})
.AsEnumerable()
.Select(x => new NumUserPosts
{
CountAllPosts = x.TrusteePost + x.PatientPost + x.SpecialistPost,
x.TrusteePost,
x.PatientPost,
x.SpecialistPost
}
The SQL query will be much more elaborate than the original SQL (for example, it involves cross joins), but it will probably still perform pretty well. AsEnumerable prevents the second part from being executed as SQL, which would bloat the SQL statement even more. It simply runs in memory.
I consider this a hack because the first part, context.Posts.Take(1) doesn't really have any meaning, it's only there to serve as a wrapper for the three separate queries. It's poor man's query packaging.
It looks like you're doing group joins instead of left outer joins (see this page).
A left outer join looks more like:
var res = from p in context.Posts
join t in context.Trusteeships
on p.Autor.ID equals t.Trustee.ID into tGroup
from tJoin in tGroup.DefaultIfEmpty()
join pat in context.Patients
on p.Autor.ID equals pat.ID into patGroup
from patJoin in patGroup.DefaultIfEmpty()
join s in context.Specialists
on p.Autor.ID equals s.ID into sGroup
from sJoin in sGroup.DefaultIfEmpty()
select ...
Unfortunately, it doesn't seem that Linq can create a query to count elements in each column.
If you don't mind using multiple queries, you could count each separately, for example:
var trusteePost = (from p in context.Posts
join t in context.Trusteeships on p.Autor.ID equals t.Trustee.ID
select t).Count()
I'm really new with this whole Linq story, but I have a good understanding in SQL.
I need to rebuild a query from SQL to Linq. My SQL query is working perfectly and so far I have tried to do something by myself with Linq but without a good result...
Is it possible that someone could help me to translate this query from SQL to Linq?
I'm really ready to learn something new in this whole story. It would be nice if you could why is it working like that in linq.
SQL Statement
SELECT TimeReport.EntryDate
, SUM(TimeReport.Hours) AS Hours
, SUM(BillingRate.HourlyRate * TimeReport.Hours) AS Amount
FROM BillingRate
INNER JOIN Activity
ON BillingRate.BillingRateId = Activity.BillingRateIt
INNER JOIN TimeReport
ON Activity.ActivityId = TimeReport.ActivityId
RIGHT OUTER JOIN Dossier
ON TimeReport.DossierId = Dossier.DossierId
INNER JOIN LBU
ON Dossier.LBUId = LBU.LBUId
INNER JOIN BU
ON Dossier.BUId = BU.BUId
GROUP BY TimeReport.EntryDate
HAVING SUM(TimeReport.Hours) > 0
ORDER BY TimeReport.EntryDate desc
What I have tired with Linq
var x = (from br in ctx.BillingRate
join a in ctx.Activity on br.BillingRateId equals a.BillingRateIt
join tr in ctx.TimeReport on a.ActivityId equals tr.ActivityId
select br)
.GroupJoin(
(from d in ctx.Dossier
join l in ctx.LBU on d.LBUId equals l.LBUId
join b in ctx.BU on d.DossierId equals
Thanks for help and fast answer.
I appreciated every effort !!
Joins aren't necessary when you have navigation properties. Since I don't know exactly what your model looks like, use the following as a starting point and adjust to your own specifications:
// starting with Dossier handles the right outer join condition
var timeRecordQuery =
from d in ctx.Dossier
from tr in d.TimeReports
// this handles the inner join conditions after the right outer join
where d.LBUId.HasValue && d.BUId.HasValue
// project what you want to use
select new
{
EntryDate = tr.EntryDate,
Hours = tr.Hours,
// simply use navigation properties, no need to join
Amount = tr.Activity.BillingRate.HourlyRate * tr.Hours
};
var resultsQuery = from e in timeRecordQuery
// group by EntryDate
group e by e.EntryDate into g
// get sum of hours for each EntryDate value
let hours = g.Sum( x => x.Hours )
// having clause
where hours > 0
// project results
select new
{
EntryDate = g.Key,
Hours = hours,
Amount = g.Sum( x => x.Amount )
};
I have the following SQL query to return all Customers who have no OrderLines with no Parts assigned - i.e. I only want the customers within which every order line of every order has no parts assigned - (in the actual problem I am dealing with a different domain but have translated to customers/orders to illustrate the problem)
SELECT c.Customer_PK
FROM Customers c
INNER JOIN Orders o
ON c.Customer_PK = o.Customer_FK
LEFT OUTER JOIN OrderLines l
ON o.Order_PK = l.Order_FK
LEFT OUTER JOIN Parts p
ON l.OrderLine_PK = p.OrderLine_FK
GROUP BY c.Customer_PK
HAVING COUNT(p.Part_PK) = 0
The best I have come up with in LINQ is as follows:
Dim qry =
(From c In context.Customers
Select New With { c.Customer_PK,
.CountParts =
(From o In c.Orders
From l In o.OrderLines
Select l.Parts.Count).DefaultIfEmpty.Sum})
qry = (From grp In qry
Where grp.CountParts = 0
Select grp.Customer_PK)
This works but generates less than optimal SQL - it is doing a subquery for Count on each row of the customers query rather than using Group By and Having. I tried making the LINQ Group By syntax work but it kept putting the filter as a WHERE not a HAVING clause.
Any ideas?
Edit in response to Answers below:
I am accepting JamieSee's answer as it addresses the stated problem, even though it does not produce the GROUP BY HAVING query I originally had.
Thanks Peter and Nick for your input on this. I am a VB developer so I have had a crack translating your code to VB, this is the closest I got to but it does not quite produce the desired output:
Dim qry = From c In context.Customers
Group Join o In context.Orders On c.Customer_PK Equals o.Customer_FK
Into joinedOrders = Group
From jo In joinedOrders.DefaultIfEmpty
Group Join l In context.OrderLines On jo.Order_PK Equals l.Order_FK
Into joinedLines = Group
From jl In joinedLines.DefaultIfEmpty
Group c By Key = New With {c.Customer_PK, jl} Into grp = Group
Where Key.jl Is Nothing OrElse Not Key.jl.Parts.Any
Select c.Customer_PK
The problem I had is that I have to push "jl" into the Group By "Key" so I can reference it from the Where clause, otherwise the compiler cannot see that variable or any of the other variables appearing before the Group By clause.
With the filter as specified I get all customers where at least one order has lines with no parts rather than only customers with no parts in any order.
Given that you don't care about the counts, only the resulting customers, consider the folllowing restatement of the problem:
Identify all Customers who do not have any Orders that have Lines with Parts.
This yields:
var customersWithoutParts = from c in Context.Customers
where !(from o in Context.Orders
from l in o.Lines
from p in l.Parts
select o.Customer_FK).Contains(c.Customer_PK)
select c.Customer_PK;
This should yield emitted SQL that is roughly equivalent to the following:
SELECT c.Customer_PK
FROM Customers AS c
WHERE (NOT EXISTS
(SELECT o.Cusomer_FK
FROM Orders AS o INNER JOIN
OrderLines AS l ON o.Order_PK = l.Order_FK INNER JOIN
Parts AS p ON l.OrderLine_PK = p.OrderLine_FK
WHERE (o.Customer_FK = c.Customer_PK)))
To get the SQL you were trying to reproduce, I'd start by trying the following:
var customersWithoutParts = from c in Context.Customers
from o in c.Orders.DefaultIfEmpty()
from l in o.Lines.DefaultIfEmpty()
join part in Context.Parts on part.OrderLine_FK equals l.OrderLine_PK into joinedParts
where joinedParts.Count() == 0
select c.Customer_PK;
Note that in VB the join here would be replaced by Group Join.
Just a tipp bocuse hard to create a query without the generated modells (C#):
from o in dc.Orders
join jOrderLines in dc.OrderLines on o.Order_PK equals jOrderLines.Order_FK into joinedOrderlines
from l in joinedOrderLines.DefaultIfEmpty()
group o by o.Customer_FK into g
where l == null || l.Count(x => x.Parts) == 0
select g.Key
What about something like this:
var qry = from c in db.Customers
join o in db.Orders.Where(x => x.Customer_FK == c.Customer_PK)
join l in db.OrderLines.Where(x => x.Order_FK = o.Order_PK).DefaultIfEmpty()
join p in db.Parts.Where(x => x.OrderLine_FK = l.OrderLine_PK).DefaultIfEmpty()
group c by new
{
c.Customer_PK
} into g
where g.Count(p => p.Part_PK != null) == 0
select new
{
Customer_PK = g.Key.Customer_PK
};
I have a SQl query working fine, I need to convert it to LINQ and got some problems.
My working SQL query:
select d.UserID, d.Content, d.UpdateTime
from DiaryPosts as d
where d.UserID = 2
/* friends */
Union
select d.UserID, d.Content, d.UpdateTime
from DiaryPosts as d
join Friends as fr
on d.UserID = fr.FriendID
where fr.UserID = 2
/* following */
Union
select d.UserID, d.Content, d.UpdateTime
from DiaryPosts as d
join Followers as fl
on d.UserID = fl.UserID
where fl.FollowerID = 2
/* ordenando por UpdateTime desc */
order by 3 desc
What I tried based on my own previous question:
var friends = (from u in db.User
join f in db.Friends
on u.ID equals f.FriendID
where f.UserID == userset.ID
orderby u.Nickname
select new FriendsSet { ID = u.ID, Nickname = u.Nickname, Thumbnail = u.Thumbnail }).ToList();
var followers = (from u in db.User
join f in db.Followers
on u.ID equals f.FollowerID
where f.UserID == userset.ID
orderby u.Nickname
select new FriendsSet { ID = u.ID, Nickname = u.Nickname }).ToList();
var diaryPosts = (from d in db.DiaryPosts
join e in db.EstadosDeAlma
on d.EstadosDeAlmaID equals e.ID
join u in db.User
on d.UserID equals u.ID
where d.UserID == userset.ID
select new DiaryPostsSet {
PostID = d.ID,
EstadoDeAlmaID = e.ID,
EstadoDeAlma = e.Title,
Author = u.Nickname,
Thumbnail = u.Thumbnail,
UserID = u.ID,
IsDuplicated = d.IsDuplicated,
FriendID = d.FriendID,
FriendName = u.Nickname,
Time = d.UpdateTime,
MessagesCount = d.FriendMessages.Count(m => m.DiaryPostsID == d.ID)
}).Take(6).ToList();
var diaryPostsUnion = diaryPosts.Union(friends).Union(followers).OrderBy(d => d.UpdateTime);
The errors I get:
'System.Collections.Generic.List<MvcWebRole1.Models.DiaryPostsSet>' does not contain a definition for 'Union' and the best extension method overload 'System.Linq.ParallelEnumerable.Union<TSource>(System.Linq.ParallelQuery<TSource>, System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments
Instance argument: cannot convert from 'System.Collections.Generic.List<MvcWebRole1.Models.DiaryPostsSet>' to 'System.Linq.ParallelQuery<MvcWebRole1.Models.FriendsSet>'
You're trying to union a sequence of one type with a sequence of another type. That doesn't make sense - unions have to be over the same types, basically.
It's not really clear what you're trying to do here, but I suspect you don't want a union.
Oh, and if you want all of this to happen in the database, get rid of the ToList calls.
EDIT: Okay, it's hard to see exactly how your LINQ queries correspond to your SQL queries, but basically your "select" clauses should be something like:
from d in db.DiaryPosts
// stuff here
select new { UserID = d.ID, d.UpdateTime, d.Content }
Use the exact same anonymous type in each query - the same property names and types, in the same order - and you should be able to use the Union method.