Linq+File

Can you give me some tips ? I only created seed file info writer to create and fill file with data (Step 1 and 2) Let's make two txt.file 1) Customers.txt 2) Orders.txt Customers table 3: CustomerID, CustomerName 1|Lasha 2|Beka 3|Dimitri Orders table : OrderID, Date, Product, Price, CustomerID 1|20201212|Coca-cola|3.0|1 2|20201111|Pepsi|3.0/2 3|20200909|Staropramen|4.0/2 4|20201212|Argo|2.0|3 There must be at least 5 users and at least 20 orders. (Fill in the details as you wish) We need to extract the following type of report: 1. The number of orders for each customer. For example: CustomerID - 1, Order Count-2 The total price of each customer's orders. For example: CustomerID - 1, SumAmount -2 10.5 3. Minimum order price for each customer. For example: CustomerID - 1, MinAmount-0.6 4. Only customers who have more than one order. CustomerID-1, Order Count-2 5. Only customers whose average order is greater than 10. CustomerID-1, AvgAmount-12.5 Best solution is with Aggregate functions in Ling, as well as Having clauses in Ling. Classic Foreach should only be used when printing.
8 Replies
この世界には 人の運命をつかさどる 何らかの超越的な 「律」
i can't understand one thing i need to take data from file like this ?
var data2 = File.ReadAllLines(path);
var data2 = File.ReadAllLines(path);
Pobiega
Pobiega2w ago
That seems reasonable, yes. The way LINQ works is that it works on sequences, and if you use that method your files are read into string arrays, aka a sequence of strings
この世界には 人の運命をつかさどる 何らかの超越的な 「律」
var customers = data.Select(line => line.Split('|')).Select(m => new Customer{
CustomerId = int.Parse([0]),
CustomerName = m[1]
});
var customers = data.Select(line => line.Split('|')).Select(m => new Customer{
CustomerId = int.Parse([0]),
CustomerName = m[1]
});
i'll make the same for orders and then join them by ID and after i'll make main task yes it's ugly code , but i'm not using AI and struggling by myself (i've some knowledge , but s*ck at basics ...)
Pobiega
Pobiega2w ago
No thats fine not ugly at all, pretty standard for "linq golf" 🙂 you missed an m on the int.Parse btw 😛
Angius
Angius2w ago
Though, if you want to make LINQ pretty, newlines are your friend You usually see
var foo = bar
.Where(x => ...)
.Select(x => ...)
.OrderBy(x => ...)
.GroupBy(x => ...)
.FirstOrDefault(x => ...);
var foo = bar
.Where(x => ...)
.Select(x => ...)
.OrderBy(x => ...)
.GroupBy(x => ...)
.FirstOrDefault(x => ...);
rather than
var foo = bar.Where(x => ...).Select(x => ...).OrderBy(x => ...).GroupBy(x => ...).FirstOrDefault(x => ...);
var foo = bar.Where(x => ...).Select(x => ...).OrderBy(x => ...).GroupBy(x => ...).FirstOrDefault(x => ...);
この世界には 人の運命をつかさどる 何らかの超越的な 「律」
The number of orders for each customer. For example: CustomerID - 1, Order Count-2 For this i wanted to group orders by customer ids and then count for each customer , but since using foreach is forbidden i'm confused
Pobiega
Pobiega2w ago
wdym? you dont need to group and even if you did, you wouldnt need a foreach
この世界には 人の運命をつかさどる 何らかの超越的な 「律」
var orderCount = orders.GroupBy(o => o.CustomerId).Select(g=> new
{
CustomerId = g.Key,
OrderCount = g.Count()
}).ToList();
foreach (var order in orderCount)
{
Console.WriteLine($"Customer ID {order.CustomerId} Order Count {order.OrderCount}");
}
var orderCount = orders.GroupBy(o => o.CustomerId).Select(g=> new
{
CustomerId = g.Key,
OrderCount = g.Count()
}).ToList();
foreach (var order in orderCount)
{
Console.WriteLine($"Customer ID {order.CustomerId} Order Count {order.OrderCount}");
}
yeah you are right i was bit confused cuz couldnot get grouped values from IEnumerable<Grouping>

Did you find this page helpful?