LINQ with multiple columns and operations - c#

i'm trying to do this on LINQ:
select p.ProductID, p.ArticleCode, SUM((case h.OperationType when 0 then 1 else -1 end) * h.[Count])
from Products p
inner join StockItems s on p.ArticleCode = s.ArticleCode
inner join StockHistorical h on s.ArticleID = h.ArticleID
where h.[Date] < '23/08/2013 11:30:00'
group by p.ProductID, p.ArticleCode
I have this:
var products = (from p in e.Products
join s in e.StockItems on p.ArticleCode equals s.ArticleCode
join h in e.StockHistoricals on s.ArticleID equals h.ArticleID
where h.Date < DateTime.Parse("23/08/2013 11:30:00")
group p by p.ProductID into StockResult
select new { });
Anyone know how can i do the
SUM((case h.OperationType when 0 then 1 else -1 end) * h.[Count])
with LINQ?
Thanks!
I forgot to say that the problem is the "group by", because i can't access the OperationType property in the StockResult group.
Solved! The key is the:
let combined = new
{
Product = p,
HistoryCount = (h.OperationType == 0 ? 1 : -1) * h.Count
}

...
let combined = new
{
Product = p,
HistoryCount = (h.OperationType == 0 ? 1 : -1) * h.Count
}
group combined by combined.Product.ProductID into StockResult
select new
{
ProductID = StockResult.Key,
Total = StockResult.Sum(x => x.HistoryCount)
}

It's not a direct translation, but I'd go with:
(h.Count(his => his.OperationType != 0)
- h.Count(his => his.OperationType == 0))
* h.Count()
It should be functionally equivalent, and seems to better represent what you're trying to do.

You are trying to do something like this?
I am not sure this is going to work.
select new
{
S=Sum(((h.OperationType)!=0?-1:1)*h.Count)
}

Related

How to convert SQL multiple table query to Linq

I tried to convert sql query to linq but couldn't get it correctly. Can someone help me to convert below to linq please?
SELECT stockdiary.datenew, locations.ID AS LOCATIONID, locations.NAME AS LOCATIONNAME,
products.REFERENCE, products.NAME,
products.CATEGORY, categories.NAME AS CATEGORYNAME,
products.SUPPLIER,
SUM(CASE WHEN stockdiary.UNITS <0 THEN stockdiary.UNITS ELSE 0 END) AS UNITSOUT,
SUM(CASE WHEN stockdiary.UNITS <0 THEN stockdiary.UNITS * stockdiary.PRICE ELSE 0 END) AS TOTALOUT,
SUM(CASE WHEN stockdiary.UNITS >=0 THEN stockdiary.UNITS ELSE 0 END) AS UNITSIN,
SUM(CASE WHEN stockdiary.UNITS >=0 THEN stockdiary.UNITS * stockdiary.PRICE ELSE 0 END) AS TOTALIN,
SUM(stockdiary.UNITS) AS UNITSDIFF,
SUM(stockdiary.UNITS * stockdiary.PRICE) AS TOTALDIFF
FROM stockdiary JOIN locations ON stockdiary.LOCATION = locations.ID,
products LEFT OUTER JOIN categories ON products.CATEGORY = categories.ID
WHERE products.ID = stockdiary.PRODUCT
GROUP BY locations.ID, locations.NAME, products.REFERENCE, products.NAME, products.CATEGORY, categories.NAME
ORDER BY locations.ID, categories.NAME, products.NAME
======
below is my linq query which gives me wrong result.
(from sd in Stockdiaries
join loc in Locations on sd.Location equals loc.Id
join prod in Products on sd.Product equals prod.Id
join cat in Categories on prod.Category equals cat.Id
select new
{
Location = loc.Name,
Category = cat.Name,
Reference = prod.Reference,
Product = prod.Name,
UnitsOut = sd.Units < 0 ? sd.Units:0,
TotalOut = sd.Units < 0 ? sd.Units * sd.Price:0,
UnitsIn = sd.Units >= 0 ? sd.Units:0,
TotalIn = sd.Units >= 0 ? sd.Units * sd.Price:0,
UnitsDiff = sd.Units,
TotalDiff = sd.Units * sd.Price
})
You've got some old join syntax in that original sql! This is in addition to the left join. This is abbreviated here:
from stockdiary
join locations on stockdiary.location = locations.id
, products
left join categories on products.category = categories.id
where products.id = stockdiary.product
See if the following approach works out for you. It may not output exactly as you desire it, but it's hopefully close and sufficient for you to work with it after that. Although that being said, I don't have any sample data from you on which to test it, so the only thing I can confirm right now is that it won't error out.
I've got comments within the code describing its components.
var query =
from sd in Stockdiaries
join loc in Locations on sd.Location equals loc.Id
// Your old syntax join should work like an inner join
join prod in Products on sd.Product equals prod.Id
// This is a left join. So you've got to do the 'into' hoop and
// then 'overwrite' the cat table.
join cat in Categories on prod.Category equals cat.Id into pCat
from cat in pCat.DefaultIfEmpty()
// put it all together into one result set
select new {
Location = loc.Name,
Category = cat?.Name, // Because it's a left join, it may be null, hence the '?'
Reference = prod.Reference,
Product = prod.Name,
sd.Units,
sd.Price
} into cnd
// group as appropriate, and remember that in linq
// grouping is a separate operation from aggregation
group cnd by new { cnd.Location, cnd.Reference, cnd.Product, cnd.Category } into g
// aggregate
select new {
g.Key.Location,
g.Key.Reference,
g.Key.Product,
g.Key.Category,
UnitsOut = g.Sum(row => row.Units < 0 ? row.Units : 0),
TotalOut = g.Sum(row => row.Units < 0 ? row.Units * row.Price : 0),
UnitsIn = g.Sum(row => row.Units >= 0 ? row.Units : 0),
TotalIn = g.Sum(row => row.Units >= 0 ? row.Units * row.Price : 0),
UnitsDiff = g.Sum(row => row.Units),
TotalDiff = g.Sum(row => row.Units * row.Price)
};
query.Dump();
#pwilcox and all other friends,
Here is the final linq query which gave me the absolute result! I added orderby expression in addition to the above query.
(from sd in Stockdiaries
join loc in Locations on sd.Location equals loc.Id
join prod in Products on sd.Product equals prod.Id
join cat in Categories on prod.Category equals cat.Id into pCat
from cat in pCat.DefaultIfEmpty()
select new {
Location = loc.Name,
Category = cat.Name,
Reference = prod.Reference,
Product = prod.Name,
sd.Units,
sd.Price
} into cnd
group cnd by new {cnd.Location,cnd.Reference, cnd.Product, cnd.Category} into g
orderby g.Key.Location, g.Key.Category, g.Key.Product
select new {
g.Key.Location,
g.Key.Reference,
g.Key.Product,
g.Key.Category,
UnitsOut = g.Sum(row => row.Units < 0 ? row.Units : 0),
TotalOut = g.Sum(row => row.Units < 0 ? row.Units * row.Price : 0),
UnitsIn = g.Sum(row => row.Units >= 0 ? row.Units : 0),
TotalIn = g.Sum(row => row.Units >= 0 ? row.Units * row.Price : 0),
UnitsDiff = g.Sum(row => row.Units),
TotalDiff = g.Sum(row => row.Units * row.Price)
})

Convert T-SQL statement into LINQ expression

i am quite new to linq and actually fighting to convert the following t-sql statement into an linq to sql or linq to entities expression:
SELECT TOP 1
vm.EventID,
vmm.VotingMatrixID,
PersonAcceptedCount = SUM( CAST( vmm.IsAccepted AS INT) )
FROM VotingMatrixMember vmm
JOIN VotingMatrix vm
ON vmm.VotingMatrixID = vm.ID
WHERE vm.EventID = 'abbe3077-24de-45d8-ac04-13dba97c1567'
AND vm.Deleted = 0
AND vmm.Deleted = 0
GROUP BY vm.EventID, vmm.VotingMatrixID
ORDER BY PersonAcceptedCount DESC
Try this, can not test
var firstItem = (
from vmm in db.VotingMatrixMember
join vm in db.VotingMatrix on vmm.VotingMatrixID equals vm.ID
where vm.EventID = "abbe3077-24de-45d8-ac04-13dba97c1567"
&& vm.Deleted = 0
&& vmm.Deleted = 0
group new {vm, vmm} by new {vm.EventID, vmm.VotingMatrixID} into gr
select new
{
EventID = gr.Key.EventID,
VotingMatrixID = gr.Key.VotingMatrixID,
PersonAcceptedCount = gr.Sum(x => Convert.ToInt32(x.IsAccepted))
}
into groupedItem
orderby groupedItem.PersonAcceptedCount descending
select groupedItem
).FirstOrDefault();
var query =
from vm in dataContext.VotingMatrices
where vm.EventId == eventId
where vm.Deleted == 0
from vmm in vm.VotingMatrixMembers
where vmm.Deleted == 0
group vmm by new { vm.EventId, vmm.VotingMatrixId } into g
select new
{
g.Key.EventId,
g.Key.VotingMatrixId,
PersonAcceptedCount: g.Select(x => Convert.ToInt32(x.IsAccepted)).Sum()
} into resultRow
order by resultRow.PersonAcceptedCount descending
select resultRow;
var row = query.FirstOrDefault();

Get parameters outside of Grouping Linq

I need to get some Field's out of the Group using linq for example, this is my code:
(from PaymentTypes in PaymentTypes_DataTable.AsEnumerable()
join CashTransactions in CashTransactions_DataTable.AsEnumerable()
on PaymentTypes.Field<Int32>("cashpaymenttype_id")
equals CashTransactions.Field<Int32>("cashpaymenttype_id")
into JoinedCashTransactions
from CashTransactions in JoinedCashTransactions.DefaultIfEmpty()
group CashTransactions by PaymentTypes.Field<Int32>("cashpaymenttype_id")
into GroupPaymentTypes
select new
{
cashpaymenttype_id = 0, // Get PaymentTypeID
cashpaymenttype_name = "", // Get PaymentTypeName
cashtransaction_amount = GroupPaymentTypes.Sum(a =>
a != null
? (a.Field<Int32>("cashtransactionstatus_id") == 1 ||
a.Field<Int32>("cashtransactionstatus_id") == 3 ? 1 : -1) *
a.Field<Double>("cashtransaction_amount")
: 0.00),
}).Aggregate(PaymentTypesTransactions_DataTable, (dt, result) => {
dt.Rows.Add(result.cashpaymenttype_id, result.cashpaymenttype_name,
result.cashtransaction_amount); return dt; });
This linq works but i need to get the fields cashpaymenttype_id and cashpaymenttype_name they are within PaymentTypes
Assuming PaymentTypeID - PaymentTypeName is 1-to-1, you could change your group by to this:
group CashTransactions by new
{
PaymentTypeId = PaymentTypes.Field<Int32>("cashpaymenttype_id"),
PaymentTypeName = PaymentTypes.Field<String>("cashpaymenttype_name")
}
into GroupPaymentTypes
The your select will look like this:
select new
{
cashpaymenttype_id = GroupPaymentTypes.Key.PaymentTypeId,
cashpaymenttype_name = GroupPaymentTypes.Key.PaymentTypeName,
...
}

SQL into LINQ to Entities

I have a big problem.
I'm for last 5 year SQL-boy, but now I need to convert my SQL query into LINQ to entity C# format.
Because I'm new in LINQ (complex statements) right now I need fast help.
Thank's in advance.
P.S. also I need some advices, some start point to start rapidly to learn LINQ to entities.
Here is my SQL (direct from my app(#endDate,#endDate and #glChartID remain as parameters also in my c# app)):
SELECT budget.accountid,
budget.perioddate,
budget.lastyear,
budget.thisyear,
budget.budget,
budget.operplan,
budget.forecast,
glchart.accounttype,
glchart.headertype
FROM budget INNER JOIN glchart ON budget.accountid = glchart.id
WHERE budget.accountid = #glChartID AND budget.perioddate BETWEEN #startDate and #endDate AND glchart.headertype NOT LIKE 'Header%'
UNION
SELECT glchart.id,
budget.perioddate,
SUM(ISNULL(budget.lastyear, 0)),
SUM(ISNULL(budget.thisyear, 0)),
SUM(ISNULL(budget.budget, 0)),
SUM(ISNULL(budget.operplan, 0)),
SUM(ISNULL(budget.forecast, 0)),
glchart.accounttype,
glchart.headertype
FROM budget INNER JOIN glchart ON budget.accountid = glchart.id
WHERE budget.accountid
IN
(SELECT g.id FROM glchart g
WHERE g.code >= glchart.code AND g.code <
CASE
WHEN glchart. headerlevel = 1 AND
(SELECT MAX(g3.code)
FROM glchart g3
WHERE g3.headerlevel = 1
) = glchart.code
THEN
(SELECT MAX(g2.code)
FROM glchart g2
WHERE g2.code >= g.code)
ELSE
(SELECT MIN(g2.code)
FROM glchart g2
WHERE g2.code > glchart.code AND
g2.headerlevel = glchart. headerlevel) END ) AND
glchart.id = #glChartID AND
budget.perioddate BETWEEN #startDate AND #endDate AND
glchart.headertype LIKE 'Header%'
GROUP BY glchart.id, budget.perioddate, glchart.accounttype, glchart.headertype
Until today, I managed (thanks to DOK)how to do it and this is how my LINQ is look like right now:
var query = ((ObjectQuery<Budget>)(
from budgets in this.ObjectContext.Budgets
join glcharts in this.ObjectContext.GLCharts on new { AccountID = budgets.AccountID } equals new { AccountID = glcharts.ID }
where
(!(from glC in this.ObjectContext.GLCharts
where Convert.ToInt16(glC.Code) >= Convert.ToInt16(glcharts.Code) && glC.Code != (Convert.ToInt64(glcharts.HeaderLevel) == 1 &&
(from g3 in this.ObjectContext.GLCharts
where Convert.ToInt64(g3.HeaderLevel) == 1
select new {g3.Code}).Max(p => p.Code) == glcharts.Code ?
(from g2 in this.ObjectContext.GLCharts
where Convert.ToInt16(g2.Code) >= Convert.ToInt16(glC.Code)
select new {g2.Code}).Max(p => p.Code) :
(from g2 in this.ObjectContext.GLCharts
where Convert.ToInt16(g2.Code) > Convert.ToInt16(glcharts.Code) && g2.HeaderLevel == glcharts.HeaderLevel
select new {g2.Code}).Min(p => p.Code))
select new {glC.ID}
).Contains(new { budgets.AccountID }) &&
glcharts.ID == 2376 && budgets.PeriodDate >= StartDate &&
budgets.PeriodDate <= EndDate &&
glcharts.HeaderType.StartsWith("Header"))
).Contains(new { budgets.AccountID }) && glcharts.ID == 2376 && budgets.PeriodDate >= StartDate && budgets.PeriodDate <= EndDate && glcharts.HeaderType.StartsWith("Header")
group new {glc = glcharts, b = budgets}
by new {
glcharts.ID,
budgets.PeriodDate,
glcharts.AccountType,
glcharts.HeaderType
} into g
select new {
AccountID = (System.Int32?)g.Key.ID,
PeriodDate = (System.DateTime?)g.Key.PeriodDate,
LastYear = g.Sum(p => ((System.Decimal?)p.t.LastYear ?? (System.Decimal?)0)),
ThisYear = g.Sum(p => ((System.Decimal?)p.t.ThisYear ?? (System.Decimal?)0)),
Budget = g.Sum(p => ((int?)p.t.Budget1 ?? (int?)0)),
OperPlan = g.Sum(p => ((System.Decimal?)p.t.OperPlan ?? (System.Decimal?)0)),
Forecast = g.Sum(p => ((System.Decimal?)p.t.Forecast ?? (System.Decimal?)0)),
AccountType = g.Key.AccountType,
HeaderType = g.Key.HeaderType
}));
return query;
But in THIS LINE: .Contains(new { budgets.AccountID }) I'm getting next error :
Error 8'System.Linq.IQueryable' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery, TSource)' has some invalid arguments
Does anybody have an idea where I'm wrong?
Thanks to everyone.
You might find some help in this excellent reference site.
That will lead you to, for example, two examples for UNION.
If you really must start out at this level of difficulty, you might consider breaking your SQL down into pieces and getting them working bit by bit. Do the first SELECT without the JOIN or WHERE, then add those one at a time. Then do the second SELECT the same way. Then add the UNION.
By the time you get this one worked out, SQL-boy, you will definitely be LINQ-man!

Linq to Sql Localisation query

Given the following tables I'd like to return the localised text for given culture or the text for the default culture where no row exist for the given culture.
diagram http://lh4.ggpht.com/_gjsCWAV_CZc/ShW6hC-eozI/AAAAAAAACbY/mXaBfiZtBY8/s400/diagram.png
So with the folowing data
Resources
ID Name
1 Donkey
2 Elephant
LocaleStrings
ID CultureID ResID LocaleText
1 1 1 Donkey
2 1 2 Elephant
3 2 1 baudet
I'd like to be able to return the following for the French culture
baudet
elephant
I've tried various queries based around LEFT JOINS samples I've seen but I'm stuck.
var ct = from r in db.Resources
join lt in db.LocaleStrings
on r.ID equals lt.ResID into res
from x in res.DefaultIfEmpty()
select new
{
CultureID = x.CultureID,
LocaleText = x.LocaleText,
ResID = x.ResID
};
var text =
from c in db.Cultures
join t in ct
on c.ID equals t.CultureID into cults
from x in cults.DefaultIfEmpty()
select x;
I'm sure there's a better way, but this seems to work:
var ct =
from c in db.Cultures
from l in db.LocaleStrings
from r in db.Resources
where r.ID == l.ResID
select new
{
CultureID = c.ID,
LocaleText = l.CultureID == c.ID ? l.LocaleText : r.Name,
ResID = r.ID,
LSID = l.CultureID == c.ID ? l.ID : 0
};
var text =
from t in ct
where t.LSID != 0 || (t.LSID == 0 && !((from ac2 in ct
where ac2.LSID > 0 && ac2.CultureID == t.CultureID
select ac2.ResID).Contains(t.ResID)))
select new
{
CultureID = t.CultureID,
LocaleText = t.LocaleText,
ResID = t.ResID
};

Categories