Sum and subtract using Linq C# - c#

I need to return only one result, I need to sum all the states (1, 3) and (2, 4, 5) then subtract each total Example:
cashtransaction_amount, cashtransactionstatus_id
50.00 1 (+)
39.99 2 (-)
330.70 3 (+)
10.00 5 (-)
50.50 4 (-)
30.00 2 (-)
Sum(1,3): 50.00 + 330.70 = 380.70
Sum(2, 5, 4): 39.99 + 10.00 + 50.50 + 30.00 = 130.49
Final Result Sum(1,3) - Sum(2, 5, 4): 380.70 - 130.49 = 250.21
I tryed:
(from DataRow Transaction_DataRow in Dt_transaction.AsEnumerable()
where Transaction_DataRow.Field<Int32>("cashpaymenttype_id") == 1 // Cash Money
group Transaction_DataRow by Transaction_DataRow.Field<Int32>("cashtransactionstatus_id") into tmp
select new
{
cashtransaction_amount = tmp.Sum(x => (x.Field<Int32>("cashtransactionstatus_id") == 1 || x.Field<Int32>("cashtransactionstatus_id") == 3) ? x.Field<Double>("cashtransaction_amount") : -x.Field<Double>("cashtransaction_amount"))
}).Aggregate(Transaction_DataTable, (dt, result) => { dt.Rows.Add(result.cashtransaction_amount); return dt; });

Have you tried
cashtransaction_amount = tmp.Aggregate((a,b) =>
{
var id = b.Field<Int32>("cashtransactionstatus_id");
var amt = b.Field<Double>("cashtransaction_amount");
return id == 1 || id == 3 ? a + amt : a - amt;
});

Another way would be:
total = tmp.Select(b =>
{
var id = b.Field<Int32>("cashtransactionstatus_id");
return (id == 1 || id == 3 ? 1 : -1) *
b.Field<Double>("cashtransaction_amount");
}).Sum();
Or:
total = tmp.Sum(b => (new[]{1,3}.Contains(b.Field<Int32>("cashtransactionstatus_id"))
? 1 : -1) * b.Field<Double>("cashtransaction_amount"));

Related

How to convert this SQL query into LINQ in c#

UPDATE m
SET m.Score = s.AScore + '-' + s.BScore
FROM #Matches m
INNER JOIN Scores s ON m.MatchId = s.MatchId
AND s.InfoTypeId = (
CASE
WHEN m.SportId = 1 AND (m.StatusId >= 13 AND m.StatusId <= 17) THEN 10
WHEN m.SportId = 1 AND m.StatusId = 20 THEN 24
WHEN m.SportId = 1 AND m.StatusId = 21 THEN 23
WHEN m.SportId = 1 AND m.StatusId = 18 THEN 8
ELSE 5
END
)
I'm having two lists in C# one is Matches and 2nd is Scores, and I want to get the result from those list like the result this query will return. Means I want to update "Score" property of "Matches" list like it is being updated in SQL query.
Any Help Please.
Matches.ForEach(m => m.Score = (Scores.Where(ms => ms.MatchId == m.MatchId
&& ms.ScoreInfoTypeId == ((m.SportId == 1 && m.StatusId >= 13 && m.StatusId <= 17) ? 10
: (m.SportId == 1 && m.StatusId == 20) ? 24
: (m.SportId == 1 && m.StatusId == 21) ? 23
: (m.SportId == 1 && m.StatusId == 18) ? 8
: 5)).Select(ms => ms.AScore + "-" + ms.BScore).FirstOrDefault()));
I have tried, but I think its too expensive. It is taking too much time. Is there any optimized way please.
Try this example in LinqPad. You can use query syntax to join the 2 lists and iterate over the result to set match scores. I used a dictionary to simplify that switch case.
void Main()
{
var matches = new[]{
new Match{MatchId=1,SportId=1,StatusId=13,Score=""},
new Match{MatchId=2,SportId=1,StatusId=18,Score=""},
new Match{MatchId=3,SportId=2,StatusId=24,Score=""},
};
var scores = new[]{
new{MatchId=1,AScore="10",BScore="0",InfoTypeId=10},
new{MatchId=2,AScore="20",BScore="0",InfoTypeId=8},
new{MatchId=3,AScore="30",BScore="0",InfoTypeId=5},
};
var dict = new Dictionary<int,int>{[13]=10,[14]=10,[15]=10,[16]=10,[17]=10,[20]=24,[21]=23,[18]=8};
var data = (from m in matches
join s in scores on m.MatchId equals s.MatchId
where s.InfoTypeId == ((m.SportId == 1 && dict.ContainsKey(m.StatusId))? dict[m.StatusId] : 5)
select new {m,s}
).ToList();
data.ForEach(o =>
{
o.m.Score = o.s.AScore + "-" + o.s.BScore;
});
matches.Dump();
}
class Match{public int MatchId; public int SportId; public int StatusId; public string Score;}

Sum multiple column in LINQ

How can I reproduce this query using linq?
SELECT
SUM(SecondsSinceLastEvent) as AccessTime,
SUM (case when DownloadType = 0 then 1 end) FileDownloadCount,
SUM (case when DownloadType = 1 then 1 end) KortextDownloadCount,
SUM (case when Action = 'print' then 1 end) PrintCountFROM EReaderLogs WHERE PublishedContent_Id = XXX
In LINQ to Entities you need to first use GroupBy before using multiple aggregate functions. To sum all the elements in a column you can group by some static key so then a single group would be a whole table:
var query = context.EReaderLogs
.Where(e => e.PublishedContent_Id == someValue)
.GroupBy(a => 1, (k, g) =>
{
AccessTime = g.Sum(e => e.SecondsSinceLastEvent),
FileDownloadCount = g.Sum(e => e.DownloadType == 0 ? 1 : 0),
KortextDownloadCount = g.Sum(e => e.DownloadType == 1 ? 1 : 0),
PrintCount = g.Sum(e => e.Action == "print" ? 1 : 0)
});

How to rewrite these LINQ statements into one query

Is it possible to use one LINQ query to do the same?
var ints = new []{1,2,3,4,5};
var odd = from i in ints where i%2==1 select i;
var even = from i in ints where i%2==0 select i;
var q = from s in new[]{""}
select new {oddCount = odd.Count(), evenCount = even.Count()};
Console.Write(q);
Edit: Want to get this
Count() already allows you to specify a predicate. So you can combine the above in one linq like this:
var ints = new[] { 1, 2, 3, 4, 5 };
Console.Write($"Odd={ints.Count(i => i % 2 == 1)}, Even={ints.Count(i => i % 2 == 0)}");
Also note that it will be considerably faster than doing a Where() as counting is easier to perform than actually returning matching elements.
Edit
If all you want is a single linq query, you could do the following clever trick:
var ints = new[] { 1, 2, 3, 4, 5 };
var Odd = ints.Count(i => i % 2 == 1);
Console.Write($"Odd={Odd}, Even={ints.Length - Odd}");
Sounds like a perfect candidate for Aggregate:
var ints = new[] { 1, 2, 3, 4, 5 };
var info = ints.Aggregate(
new { oddCount = 0, evenCount = 0 }, (a, i) =>
new { oddCount = a.oddCount + (i & 1), evenCount = a.evenCount + ((i & 1) ^ 1) });
Console.WriteLine(info);
prints
{ oddCount = 3, evenCount = 2 }
You could do it one query like this:
var q = ints.Select(i => new { Number = i, Type = (i % 2 == 0) ? "Even" : "Odd" }).GroupBy(i => i.Type).Select(g => new { Type = g.Key, Count = g.Count() });
This would return a list though, with 'Type' and 'Count', as shown below.
If you're looking for a simple object as you currently have, you can use something simpler like this:
var q = new { OddCount = ints.Count(i => i % 2 != 0), EvenCount = ints.Count(i => i % 2 == 0) };
This would be a single object with "OddCount" and "EventCount" properties.
Here's another way that does only a single iteration over the original list.
var ints = new []{1,2,3,4,5};
string[] parities = { "even", "odd" };
var result = ints
.GroupBy(i => i % 2)
.Select(g => new { Name = parities[g.Key], Count = g.Count() });
You just move your queries directly into the select
var q = from s in new[] { "" }
select new {
oddCount = (from i in ints where i % 2 == 1 select i).Count(),
evenCount = (from i in ints where i % 2 == 0 select i).Count()};
int odd = 0;
int even = 0;
(from s in ints
let evens = s % 2 == 0 ? even++ : even
let odds = s % 2 != 0 ? odd++ : odd
select true).ToList();
With this you have the values loaded in even and odd.
This approach has the advantage it only iterates the array once

Is there something like `continue` to bypass or skip an iteration of query in LINQ?

Take this example:
int[] queryValues1 = new int[10] {0,1,2,3,4,5,6,7,8,9};
int[] queryValues2 = new int[100]; // this is 0 to 100
for (int i = 0; i < queryValues2.Length; i++)
{
queryValues2[i] = i;
}
var queryResult =
from qRes1 in queryValues1
from qRes2 in queryValues2
where qRes1 * qRes2 == 12
select new { qRes1, qRes2 };
foreach (var result in queryResult)
{
textBox1.Text += result.qRes1 + " * " + result.qRes2 + " = 12" + Environment.NewLine;
}
Obviously this code will result in:
1 * 12 = 12
2 * 6 = 12
3 * 4 = 12
4 * 3 = 12
6 * 2 = 12
But what I need is only the first 3 lines. That is I do not want if 2*6 = 12 the query checks if 6*2 is also 12. Is there a way to filter this in the LINQ query or I have to do it in the foreach loop afterward?
My question just is a sample to show what I mean. so I want to know the way of doing such thing no matter what is the type of object being linqed to!
In general the simple solution would be more where conditions since the where clauses are what by definition cause LINQ to skip iterations:
var queryResult =
from qRes1 in queryValues1
from qRes2 in queryValues1
where qRes1 * qRes2 == 12
&& qRes1 <= Math.Sqrt(12)
select new { qRes1, qRes2 };
You could use .Distinct() and create your own IEqualityComparer that compares objects based on what 'equals' means in your case.
So, for your original example:
class PairSetEqualityComparer : IEqualityComparer<Tuple<int, int>>
{
public bool Equals(Tuple<int, int> x, Tuple<int, int> y)
{
return (x.Item1 == y.Item1 && x.Item2 == y.Item2) ||
(x.Item1 == y.Item2 && x.Item2 == y.Item1);
}
public int GetHashCode(Tuple<int, int> obj)
{
return obj.Item1*obj.Item2;
}
}
And, you use it like this:
var queryResult =
(from qRes1 in queryValues1
from qRes2 in queryValues2
where qRes1 * qRes2 == 12
select new Tuple<int, int>(qRes1, qRes2)).Distinct(new PairSetEqualityComparer());
TakeWhile(condition):Returns elements from a sequence as long as a specified condition is true, and then skips the remaining elements.
foreach (var result in queryResult.TakeWhile(x => x.qRes1 <= Math.Sqrt(12)))

C# Group with conditional

how can i grouping a data with conditional if bill < 10 ?
i have table:
meetingId | bill
------------------
a | 6
b | 7
c | 1
a | 5
a | 3
b | 4
g | 2
expected results :
a = 6+5+3 = 14 limit is 10 --> 10 and 4
b = 7+4 = 11 so limit is 10 --> 10 and 1
c and g not over the limit.
meetingId | bill
------------------
a | 10
a | 4
b | 10
b | 1
c | 1
g | 2
i tried in SQL why but i stuck with if condition
my SQL :
SELECT NO_ORDRE
,ORDRE.CODE_CLIENT As CodeCl
,[CODE_DEST]
,ORDRE.RS_NOM As OrdreRS
,ORDRE.ADRESSE As OrdreAdr
,ORDRE.CP As OrdreCP
,ORDRE.VILLE As OrdreVille
,ENLEV_CREMB
,ENLEV_DECL
,MODAL_MODE
,[PAYS]
,[INSEE]
,[SIRET]
,ORDRE.TEL As OrdreTel
,ORDRE.FAX As OrdreFax
,[EMAIL]
,[NBR_COLIS]
,[POID]
,[OBS]
,[DATE_CREE]
,[DATE_MODIF]
,[REF_EXPED]
,[AUTRE_REF]
,[AGENCE]
,[TRANSPORTEUR]
,NOM
,CAPITAL
,LIBELLE
,T_LOGO.IMG As FaImg
,T_LOGO.ADRESSE As FaAdr
,T_LOGO.CP As FaCp
,T_LOGO.VILLE As FaVille
,T_LOGO.TEL As FaTel
,T_LOGO.FAX As FaFax
,FAWEB_CLIENT.RS_NOM As CliRsNom
,FAWEB_CLIENT.ADRESSE As CliAdr
,FAWEB_CLIENT.CP As CliCp
,FAWEB_CLIENT.VILLE As CliVille
FROM [ORDRE]
LEFT JOIN T_LOGO ON ORDRE.TRANSPORTEUR = T_LOGO.NOID
LEFT JOIN FAWEB_CLIENT ON ORDRE.CODE_CLIENT = FAWEB_CLIENT.CODE_CLIENT
WHERE (STATUT_ORDRE = 2) AND (TRANSPORTEUR IN (SELECT ParsedString From dbo.ParseStringList(#Trans)))
and then i use in C#
List<Pers_Ordre> oListOrdre = new List<Pers_Ordre>();
while (readerOne.Read())
{
Pers_Ordre oPerOrdr = new Pers_Ordre();
Pers_Ordre test = (from t in oListOrdre where t.DestId == readerOne["CODE_DEST"].ToString() select t).FirstOrDefault();
oPerOrdr.OrdreId = Convert.ToInt32(readerOne["NO_ORDRE"]);
oPerOrdr.DestId = readerOne["CODE_DEST"].ToString();
if (test == null)
{
oListOrdre.Add(oPerOrdr);
}
else
{
int NbrColis = (from t in oListOrdre where t.DestId == readerOne["CODE_DEST"].ToString() select t.NbrColis).FirstOrDefault();
if (NbrColis < 5)
{
test.NbrColis += NbrColis;
}
}
}
it not work what i expected.
Thanks for your help!
(Not really an answer, but this doesn't fit in a comment.)
Here's a LINQ-to-Objects query that groups items by meetingId and creates new items such that there is one item with bill less than 10 and as many items as needed with bill equalling 10 to keep the sum:
Is this what you're looking for?
Code:
var list = new List<Tuple<char, int>>
{
Tuple.Create('a', 6),
Tuple.Create('b', 7),
Tuple.Create('c', 1),
Tuple.Create('a', 5),
Tuple.Create('a', 3),
Tuple.Create('b', 4),
Tuple.Create('g', 2),
};
var result = list
.GroupBy(x => x.Item1)
.Select(g => new
{
Key = g.Key,
Sum = g.Sum(x => x.Item2)
})
.Select(p => new
{
Key = p.Key,
Items = Enumerable.Repeat(10, p.Sum / 10)
.Concat(Enumerable.Repeat(p.Sum % 10, 1))
})
.SelectMany(p => p.Items.Select(i => Tuple.Create(p.Key, i)))
.ToList();
This SQL query will return the wanted results:
SELECT meetingId, SUM(bill) as bill_total
FROM table
GROUP BY meetingId
HAVING SUM(bill) < 10
You should not do this at the client side because it can get pretty intensive, a simple GROUP BY with a HAVING clause should give you the expected results:
Sample data:
The query you need:
SELECT
MeetingID,
SUM(bill) AS Total
FROM
Table_1
GROUP BY
MeetingID
HAVING
SUM(bill) < 10
The results of the query:
table.GroupBy(p => p.meetingId).Where(p => p.Sum(q => q.bill) < 10)
.Select(p => new
{
meetingId= p.Key,
bill= p.Sum(q => q.bill)
});

Categories

Resources