SQL inner select to LINQ - c#

This thing is driving me crazy.
I think its not that complicated but I don't get it.
I have this working SQL statement and I need the Linq statement for it.
select
a.id, a.date,
(select top 1 b.price from b where a.id = b.id and a.date >= b.date) as price
from a;
For explanation:
I have a table a with articles and a table b with a price history.
Now I need a datagrid where I can enter new entries for table a (so a view is not working) and after saving its showing me associated price
I hope I could express myself understandably

For translating SQL to LINQ query comprehension:
Translate FROM subselects as separately declared variables.
Translate each clause in LINQ clause order, leaving monadic operators (DISTINCT, TOP, etc) as functions applied to the whole LINQ query.
Use table aliases as range variables. Use column aliases as anonymous type field names.
Use anonymous types (new { }) for multiple columns
Left Join is simulated by using a into join_variable and doing another from from the join variable followed by .DefaultIfEmpty().
Replace COALESCE with the conditional operator and a null test.
SELECT * must be replaced with select range_variable or for joins, an anonymous object containing all the range variables.
SELECT fields must be replaced with select new { ... } creating an anonymous object with all the desired fields or expressions.
Proper FULL OUTER JOIN must be handled with an extension method.
For your query:
var ans = from ra in a
select new {
ra.id,
ra.date,
price = (from rb in b
where ra.id == rb.id && ra.date >= rb.date
select rb.price).First()
};

I'm not sure which syntax you're aiming for, but one of these should do the trick. I haven't tested it though.
from xa in a
select new
{
id,
date,
price = (
from xb in b
where xa.id == xb.id && xa.date >= xb.date
select xb.price
).First() // or .FirstOrDefault() if you want to allow null prices
};
or
a.Select(xa => new
{
id,
date,
price = b.First(xb => xa.id == xb.id && xa.date >= xb.date) // or .FirstOrDefault() if you want to allow null prices
});

Related

SQL query in linq to sql, group by trouble

The idea here is to add up all the ingredients from all of the recipes. This is my query
SELECT IngredientName,sum(Amount) as Amount,Unit
FROM RecipeIngredients Join Ingredients ON RecipeIngredients.IngredientID = Ingredients.IngredientsID
GROUP BY IngredientID,IngredientName,Unit
ORDER BY IngredientName
It works great. No issues. I'm getting confused with the linq to sql syntax.
This is what I have so far.
using (lDataContext db = new lDataContext())
{
return (from ri in db.RecipeIngredients join i in db.Ingredients on ri.IngredientID equals i.IngredientsID
group new {ri,i} by new {i.IngredientsID,i.IngredientName,ri.Unit }
into table
select new {
Name = table.IngredientName
Unit = table.Unit
Amount = table.Sum(i.amount) }
).ToList();
}
Can somone please help with my syntax?
Assuming you are not using LINQ to Entities but just LINQ to SQL,
For translating SQL to LINQ,
Translate subselects as separate variables
Translate each clause in LINQ clause order, leaving monadic operators (DISTINCT, TOP, etc) as functions applied to the whole LINQ query.
Use table aliases as range variables. Use column aliases as anonymous type field names.
Use anonymous types (new { }) for multiple columns
Left Join is simulated by using a join variable and doing another from from the join variable followed by .DefaultIfEmpty().
Replace COALESCE with the conditional operator and a null test.
SELECT * must be replaced with select range variable or for joins, an anonymous object containing all the range variables.
SELECT flds must be replaced with select new { ... } creating an anonymous object with all the desired fields or expressions.

LINQ query in C# using group and join

I am trying to write the following SQL query in LINQ C# but am not able to get hold of the columns after the group clause.
SQL query
/*get the number of questions by subject*/
select b.SubjectID, b.SubjectName, count(*) AS count
from QuestionsTable a, SubjectTable b
where a.SubjectID is not null AND a.SubjectID = b.SubjectID
GROUP BY a.SubjectID
LINQ Query
var result =(from a in db1.QuestionsTables
join b in db1.SubjectTables
on a.SubjectID equals b.SubjectID
where a.SubjectID != null
group a by a.SubjectID into g
select new { a.QuestionID, a.SubjectID, b.SubjectName
}).ToList();
Your SQL wouldn't be executed correctly cause you query groups by a.Subject.ID, but selects b.SubjectID, and b.SubjectName. Usually you should also include single selected fields to GROUP BY list.
(As far as I know, some SQL servers can process functional dependent fields, so they could process your query. But in general it's wrong).
So your working query should be:
SELECT b.SubjectID, b.SubjectName, COUNT(*) AS Count
FROM QuestionsTable a, SubjectTable b
WHERE a.SubjectID is not null AND a.SubjectID = b.SubjectID
GROUP BY b.SubjectID, b.SubjectName
And your LINQ should be
from a in db.QuestionsTable
join b in db.SubjectTable
on a.SubjectId equals b.SubjectId
where a.SubjectId != null
group b by new { b.SubjectId, b.SubjectName } into g
select new { g.Key.SubjectId, g.Key.SubjectName, g.Count() }
Although it could be made the way you asked, the same result can be obtained in much simpler natural way, not involving grouping at all, like this
// get the number of questions by subject
var result =
(from s in db1.SubjectTables
select new
{
s.SubjectID,
s.SubjectName,
Count = db1.QuestionsTables.Count(q => q.SubjectID == s.SubjectID)
}).ToList();
Update: Regardless of downvotes, I strongly believe this is the right way to solve this particular problem - simple and natural. Why group something that is already grouped (by primary key).

How to convert Group By SQL to LINQ?

I have the following SQL statement I'm trying to convert to Entity Framework.
SELECT S_NUMBER,A_NUMBER,FIRST_NAME,LAST_NAME
FROM EMPLOYEE WHERE S_NUMBER IN (
SELECT S_NUMBER
FROM EMPLOYEE
WHERE CO='ABC'
GROUP BY S_NUMBER
HAVING COUNT(*) > 1)
I've done some searching on using Group By in LINQ as well as sub-queries. I'm using LinqPad with a "C# Statement" and I came up with the following which based on some examples I found looks like it should work. However, I'm getting errors when trying to assign esn.S_NUMBER to sNumber in the anonymous object. The message says 'IGrouping' does not contain a definition for 'S_NUMBER'.
var result = from e in EMPLOYEE
where e.CO=="ABC"
group e by e.S_NUMBER into esn
select new
{
sNumber = esn.S_NUMBER
};
result.Dump();
I was under the impression that all the records would basically get put into a temp table called esn and I could be able to call the temptable.column name to assign it to my object that I will eventually return as a list.
You want to use Key instead of S_NUMBER. When grouping, the results get put into a IEnumerable<IGrouping>>. The grouping has a Key property which holds the key for that group, which in this case it's your S_NUMBER.
select new
{
sNumber = esn.Key
};
The following query should be a translation of the original SQL query. Instead of using a subquery, we're grouping and doing another from...in to "flatten" the sequence, and also checking that each grouping has a count > 1 like the original query.
var result = from e in EMPLOYEE
where e.CO=="ABC"
group e by e.S_NUMBER into esn
from e2 in esn
where esn.Count() > 1
select new
{
e.S_NUMBER,
e.A_NUMBER,
e.FIRST_NAME,
e.LAST_NAME
};
Since you're using the results of one query to filter another we can do a fairly direct transliteration of the query like so:
var result =
from e in EMPLOYEE
join f in (
from fe in EMPLOYEE
where fe.CO == 'ABC'
group null by S_NUMBER into grp
where grp.Count() > 1
select grp.Key
)
on e.S_NUMBER equals f
select new { e.S_NUMBER, e.A_NUMBER, e.FIRST_NAME, e.LAST_NAME };
Not only does this look a lot more like the original query but it should perform a bit faster (on MS SQL at least, can't speak for others) than the other form that might be simpler in LINQ but is much more complex when converted to SQL... four selects and a cross join, in my test version, vs two selects and an inner join for this one.
Of course if you prefer you can pull the inner query out as a separate IQueryable for clarity:
var filter =
from e in EMPLOYEE
where e.CO == 'ABC'
group null by S_NUMBER into grp
where grp.Count() > 1
select grp.Key;
var result =
from e in EMPLOYEE
join f in filter
on e.S_NUMBER equals f
select new { e.S_NUMBER, e.A_NUMBER, e.FIRST_NAME, e.LAST_NAME };

Subquery in Linq query giving A query body must end with a select clause or a group clause

booked - below - should be the sum of the NumberBooked column from the Bookings table - which has a link to the TourDates table on the TourDateId.
However I'm getting the error A query body must end with a select clause or a group clause
Can anyone please help me fix this query?
Thank you,
Mark
var tours = from t in Tours
join d in TourDates on t.TourId equals d.TourId
where d.Date == dt
select new
{
t.TourId,
d.TourDateId,
booked = (from b in Bookings where d.TourDateId == b.TourDateId)
Select new {bk.Sum(b.NumberBooked()}
};
I believe this:
booked = (from b in Bookings where d.TourDateId == b.TourDateId) // oops
Select new {bk.Sum(b.NumberBooked()}
should be this:
booked = (from b in Bookings where d.TourDateId == b.TourDateId // move from here
select new {bk.Sum(b.NumberBooked()}) // to here
Note that I moved the end parenthesis ) so that it comes after the select, not after TourDateId
The closing round paranthesis ends the query which needs a select at the end.
Why don't you use method syntax? It is much better readable in this case. Also, a Select is optional with .Where and method syntax:
join d in TourDates on t.TourId equals d.TourId
where d.Date == dt
select new
{
t.TourId,
d.TourDateId,
booked = Bookings.Where(b => d.TourDateId == b.TourDateId)
.Sum(b => b.NumberBooked())
};
Note that i have removed the anonymous type since you just want the sum of that column
should be the sum of the NumberBooked column from the Bookings table -
which has a link to the TourDates table on the TourDateId.

Advice on removing multiple sub-queries (which contains a join) by optimizing Linq To SQL query

I'm working on adding globalization to my product cataloge and I have made it work. However, I feel that the underlaying SQL query isn't performing as well as it could and I could need some advice on how to change my Linq To SQL query to make it more efficient.
The tables that are used
Product contains a unique id for each product. This column is called EntityID
TextTranslation contains the globalized text. The columns in this table are CultureID (a string), TextID (a reference to the text), Value (the actuall globalized text).
Text contains the mapping between a globalized text and a product. There is also a column which indicates which type of text it is (like name, description and so on)
TextType contains the definition (id, name and description) for a text type.
var culturedTexts =
from translation in ctx.TextTranslations
join text in ctx.Texts on translation.TextId equals text.TextId
where translation.CultureId == "en-EN"
select new
{
text.EntityId,
text.TextTypeId,
translation.Value,
};
var products =
from p in ctx.Products
let texts = culturedTexts.Where(i => i.EntityId == p.EntityId)
select new Model.Product
{
Description = texts.Where(c => c.TextTypeId == (int)TextType.Description).SingleOrDefault().Value,
Name = texts.Where(c => c.TextTypeId == (int)TextType.Name).SingleOrDefault().Value
};
When this is executed I get a query which looks like
SELECT (
SELECT [t1].[Value]
FROM [Common].[TextTranslation] AS [t1]
INNER JOIN [Common].[Text] AS [t2] ON [t1].[TextId] = [t2].[TextId]
WHERE ([t2].[TextTypeId] = 2) AND ([t2].[EntityId] = [t0].[EntityId]) AND ([t1].[CultureId] = 'sv-SE')
) AS [Description], (
SELECT [t3].[Value]
FROM [Common].[TextTranslation] AS [t3]
INNER JOIN [Common].[Text] AS [t4] ON [t3].[TextId] = [t4].[TextId]
WHERE ([t4].[TextTypeId] = 1) AND ([t4].[EntityId] = [t0].[EntityId]) AND ([t3].[CultureId] = 'sv-SE')
) AS [Name]
FROM [Catalog].[Product] AS [t0]
So each globalized text (Name, Description) in the LINQ query gets its own sub query and associated join. Is it possible to streamline this a bit and remove each text type getting its own join and subquery?
Well, given that they are getting different TextTypeId values, how would you prefer the TSQL to look? If you do a single JOIN, you'll have to put in a messy SELECT CASE or similar to discriminate between type "1" and type "2".
One option would be to to simply bring back all the suitable rows and do the final projection in-memory at the client, but to be honest I expect that the SQL optimizer will make light work of that TSQL anyway... especially if that query hits a good spanning index.

Categories