score:1
we can do this by using linq. please find the below code snippet in c#. hope it helps. here we are grouping by year and month and then summing up the amount as well.
namespace solutions
{
using system;
using system.collections.generic;
using system.linq;
public class security
{
public guid id { get; set; }
public string name { get; set; }
public string quatation { get; set; }
//public securitytype securitytype { get; set; }
public double denomination { get; set; }
//public currencytype demoniationtype { get; set; }
public virtual icollection<reportperiod> reportperiods { get; set; }
}
public class reportperiod
{
public guid id { get; set; }
public datetime start { get; set; }
public datetime end { get; set; }
public guid securityid { get; set; }
public guid stockexchangeid { get; set; }
public double amount { get; set; }
public virtual security security { get; set; }
}
public class entities
{
public static void main(string[] args)
{
security security = new security()
{
id = guid.newguid(),
denomination = 1,
name = "a",
quatation = "z",
reportperiods = new list<reportperiod>()
};
security.reportperiods.add(new reportperiod()
{
amount = 10,
security = security,
securityid = security.id,
end = datetime.now.adddays(1),
start = datetime.now,
id = guid.newguid(),
stockexchangeid = guid.newguid()
});
security.reportperiods.add(new reportperiod()
{
amount = 5,
security = security,
securityid = security.id,
end = datetime.now.adddays(1),
start = datetime.now,
id = guid.newguid(),
stockexchangeid = guid.newguid()
});
security.reportperiods.add(new reportperiod()
{
amount = 5,
security = security,
securityid = security.id,
end = datetime.now.adddays(1),
start = datetime.now.addmonths(-1),
id = guid.newguid(),
stockexchangeid = guid.newguid()
});
foreach (var groupedreportvalues in security.reportperiods
.groupby(period => new { period.start.year, period.start.month }).select(
groupedonmonth => new
{
startyear = groupedonmonth.key.year,
startmonth = groupedonmonth.key.month,
amountsum = groupedonmonth.sum(reportvalue => reportvalue.amount)
}))
{
console.writeline(groupedreportvalues.startyear);
console.writeline(groupedreportvalues.startmonth);
console.writeline(groupedreportvalues.amountsum);
console.writeline();
}
console.readline();
}
}
}
score:1
you want general amount of month.
- assuming you want it in the format
dictionary<datetime, double>
wherekey
is first date of month (of which month we have general amount invalue
). - assuming that, range of
start
andend
doesn't go across the month.
add this property in your security
class.
public dictionary<datetime, double> amountgroupedbymonth
{
get
{
dictionary<datetime, double> table = new dictionary<datetime, double>();
if (reportperiods != null && reportperiods.count > 0)
{
reportperiod frtreportperiod = reportperiods.first();
datetime monthstdt =
new datetime(frtreportperiod.start.year, frtreportperiod.start.month, 1);
double groupedamount = 0;
foreach (reportperiod reportperiod in reportperiods)
{
//checking if this report should be grouped with pervious report or not
if (monthstdt.year == reportperiod.start.year
&& monthstdt.month == reportperiod.start.month)
{
groupedamount += reportperiod.amount;
}
else
{
//if we find that this report is of different month.
table.add(monthstdt, groupedamount);
groupedamount = reportperiod.amount;
monthstdt =
new datetime(reportperiod.start.year, reportperiod.start.month, 1);
}
}
if (groupedamount != 0 && !table.containskey(monthstdt))
table.add(monthstdt, groupedamount);
}
return table;
}
}
by adding this property, month wise grouped data will be easily available to the object of security
. and also, as it not stored in any variable, you will not be required to update it (or generate) before using it. simply call this property and it will calculate general amount month wise with latest available data.
security s = new security();
datetime nowdate = datetime.now;
s.reportperiods = new list<reportperiod>();
for(int i = 0; i <= 70; i = i + 5)
{
s.reportperiods.add(new reportperiod(nowdate.adddays(i), nowdate.adddays( i + 3), 200 ));
}
dictionary<datetime, double> amountgroupedbymonth = s.amountgroupedbymonth;
and output will be like:
Source: stackoverflow.com
Related Query
- How select rows with grouping by month
- How do I select rows from database elegantly with multiple conditions (which could be null)?
- How to select Parents rows and Child rows with only specific columns
- How can I do a group by on rows with a day of month for each in the group?
- Generic code to determine how many rows from a table are in a different table with matching structure?
- How to select records which do not have rows with dates falling in the current week
- How to use LINQ to select object with minimum or maximum property value
- How to select only the records with the highest date in LINQ
- How can I do SELECT UNIQUE with LINQ?
- What does this C# code with an "arrow" mean and how is it called?
- How to delete rows from DataTable with LINQ?
- How to select top N rows in a Linq GroupBy
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- How do I select items from an array using an array of indices with Linq?
- EF Core how select entity with many-to-many relationship
- How can I select items with Linq by Date while ignoring Time portion of a DateTime property?
- How to Select All with a One to Many Relationship Using Linq
- How to select rows from DataTable based on Index / Row Number?
- Select all rows with distinct column value using LINQ
- How can I group by the difference between rows in a column with linq and c#?
- How do I define a SELECT TOP using LINQ with a dynamic query?
- How to select each splited string and group by with another members in linq?
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- DataTable select with LiNQ and check is there duplicate rows or not
- Efficiently select random rows from large resultset with LINQ (ala TABLESAMPLE)
- How to use a LINQ query to include index in Select with F#
- How to Select only Id's with linq when there is List in List
- How do I do this SELECT statement that uses column renaming (or column alias) with LINQ?
- how to delete multiple rows of data with linq to EF using DbContext
- How merge expression in the Select method with Linq
More Query from same tag
- {"LINQ to Entities does not recognize the method 'Boolean IsLetter(Char)' method, and this method cannot be translated into a store expression."}
- Entity Framework / LINQ: Left join defaultifempty fails
- Why am I getting an IndexOutOfRangeException here?
- C# Listbox Filter with Linq fulltext search query
- C# Linq Consolidate by ID and Sum Quantities
- Linq and SubSonic - returning nested complex types
- LINQ Group by SUM
- How to return just first parent element, but many children and grandchildren in Linq with Entity Framework - SYBASE ASE Connector bug
- Entity Framework: select property as Object
- Linq Intermediate Object in method chain for Where and Select?
- cannot assign to X because it is a 'method group'
- LINQ Expression GroupBy Select First
- How to select 'product' rows instead of 'sale' rows
- Set Attribute value in LINQ to XML
- Entity Framework Data Transfer Objects Best Practice
- .Max() method giving error when used in an if else
- List is empty after linq operation
- LINQ GroupBy return dictionary with given objects as value
- Not understanding LINQ query
- C# How to turn <T> and Expression<Func<T, string>> selector into T.Select( selector)?
- LINQ with ManyToMany: Filtering based on multiple selection
- How do I get the second element in a collection, using LINQ?
- What does "object is enumerated" mean in C#?
- Linq TotalCount of "Grandchildren" with null checks and property filters
- Is there a secret to using LINQ to SQL to add records when the object has relationships?
- SubSonic 3 ActiveRecord lambda expression partially ignored on delete
- C# and displaying a row randomly for a sight word game
- Convert IAsyncEnumerable to List
- Loop through section without pulling all elements of the same name
- VB.NET LINQ WHERE query collection with various keys throws KeyNotFoundException