score:1
Accepted answer
//reading the csv and create anonymous object for each line
var inputEntries = File.ReadLines(@"C:\\Test\\debitorders.csv")
.Select(line =>
{
var values = line.Split(',');
return new
{
AccountHolder = values[0].Trim().Substring(0,1) + values[1].Trim(),
AccountNumber = long.Parse(values[2].Trim()),
AccountType = values[3].Trim(),
BankName = values[4].Trim(),
Branch = values[5].Trim(),
Amount = 100 * double.Parse(values[6].Trim()),
Date = DateTime.Parse(values[7].Trim())
};
});
var banks = inputEntries
.OrderBy(e => e.BankName)
.GroupBy(e => e.BankName, e => e);
//output data
foreach(var bank in banks)
{
//output bank header
var AccountName = bank.Key;
if (AccountName.Length >= 16)
{
AccountName = AccountName.Substring(0, 16);
}
else
{
AccountName += new string(' ', 16 - AccountName.Length);
}
var NumberOfAccounts = bank.Count();
var TotalAmount = bank.Select(acc => acc.Amount).Sum();
var Header = NumberOfAccounts.ToString("000") + TotalAmount.ToString("0000000000");
Console.WriteLine(Header);
//sort accounts
var sortedAccounts = bank
.OrderBy( acc=> acc.AccountHolder)
.OrderByDescending(acc => acc.Amount);
//output accounts
foreach( var account in sortedAccounts)
{
var outputLine =
account.AccountHolder +
account.AccountNumber +
account.AccountType +
account.Amount +
account.Date.ToShortDateString();
Console.WriteLine(outputLine);
}
}
Source: stackoverflow.com
Related Query
- Sorting a DataTable by columns in a file
- c # using linq to group by multiple columns in a datatable
- How to group by on multiple columns from datatable with linq?
- C# DataTable select columns where column name like
- LINQ to Datatable Group by and return all columns
- How to get 2 columns from datatable in linq
- Sorting rows in datatable
- DataTable Linq join many columns
- LINQ: Sorting on many columns dynamically
- Remove columns from datatable
- How to select all columns in LINQ Datatable join?
- Finding common columns from two datatable and using those for Join condition in LINQ
- How to select multiple columns from datatable in linq group by?
- LINQ - Group DataTable by multiple columns determined at runtime
- Dynamic LINQ to join on dynamic columns on DataTable
- How to select Columns using where condition from one DataTable to another in C#
- filter a datatable to contain unique columns
- Sorting file names in a directory giving wrongly ordered results
- Group DataTable by multiple columns and concatenate strings
- DataTable remove columns and re-order the columns
- How to use LINQ to get unique columns from a DataTable
- Sorting a DataTable using LINQ, when sort-by-columns may vary
- DataTable Column value grouping and sorting
- C# Linq How to select the distinct row count of multiple columns in a datatable
- How to get columns of both datatable after innerjoining them using linq
- C# - Sorting a datatable by a date column and only binding top 10 records to grid
- Efficiently Filtering One DataTable matching multiple columns of another DataTable in C#
- Distinct all columns in a datatable and store to another datatable with LINQ
- DataTable select distinct values from several columns
- Specify Which Columns Are In A Datatable
More Query from same tag
- How to find the second latest file in folder
- ToList does not shortcut if input is already a list
- delete item in a list c#
- Why is this linq query with anonymous type faster than anything else I try?
- LINQ CROSS JOIN Error
- How can I assign a multiple column in my sql tables to single column in my List<>
- How do I search for a phrase in a piece of text in C#?
- LINQ's pathetic handling of composite keys
- Executing stored procedure using .dbml file LINQ
- Join navigation property with line
- Simple linq question: How to filter a source afterwards?
- Calculate elapsed time in a linq query
- How to use GroupBy() in subqueries with LINQ?
- Query table by filtering criteria from rows
- Retrieve strings from a file, filtering with Linq when multiple lines contain the exact same string
- Overriding or ignoring undeclared entities in C# using LINQ
- SQL datetime and calendar.selectedDate in asp format issue
- Foreach String variable (not Value) in a List<>
- linq to json target specific items to deserialize
- Summarize a property based on a sub collection
- XML class not recognizing its attributes
- How to convert Linq.ParallelQuery to Linq.IQueryable
- Generate linq classes using sqlmetal for npgsql
- Order a list of lists in C# using LINQ
- search database table record using two parameters in linq
- Is AsQueryable method departed in new Mongodb C# driver 2.0rc?
- NHib 3 Configuration & Mapping returning empty results?
- Compare local list to DataBase
- LINQ query with Multiple Tables?
- LINQ Join on multiple datasets with NULL elements