linq group min max

main
syneffort 2 years ago
parent 54944d5a16
commit ea54dc68fb
  1. 24
      DevTips/.vscode/launch.json
  2. 41
      DevTips/.vscode/tasks.json
  3. 22
      DevTips/DevTips.sln
  4. 10
      DevTips/DevTips/DevTips.csproj
  5. 14
      DevTips/DevTips/Program.cs
  6. 101
      DevTips/DevTips/Tips/LinqGroupMinMax.cs

@ -0,0 +1,24 @@
{
// IntelliSense .
// .
// https://go.microsoft.com/fwlink/?linkid=830387() .
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/DevTips/bin/Debug/net7.0/DevTips.dll",
"args": [],
"cwd": "${workspaceFolder}/DevTips",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}

@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/DevTips/DevTips.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/DevTips/DevTips.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/DevTips/DevTips.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevTips", "DevTips\DevTips.csproj", "{E0D7D3A3-0C0F-4499-BD11-13336302EB8D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E0D7D3A3-0C0F-4499-BD11-13336302EB8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E0D7D3A3-0C0F-4499-BD11-13336302EB8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0D7D3A3-0C0F-4499-BD11-13336302EB8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0D7D3A3-0C0F-4499-BD11-13336302EB8D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,14 @@
using DevTips.Tips;
namespace DevTips;
class Program
{
static void Main(string[] args)
{
LinqGroupMinMax.Print();
System.Console.WriteLine();
LinqGroupMinMax.Client1();
System.Console.WriteLine();
LinqGroupMinMax.Client2();
}
}

@ -0,0 +1,101 @@
using System.Data;
using System.Collections.Generic;
using System.Linq;
namespace DevTips.Tips
{
class LinqGroupMinMax
{
public static void Print()
{
DataTable table = GetData();
foreach (DataRow row in table.Rows)
{
System.Console.WriteLine($"\t{row["GroupID"]}\t{row["SalesPerson"]}\t{row["Sales"]}");
}
}
public static void Client1()
{
var data = GetData().AsEnumerable();
var result = from row in data
group row by row.Field<int>("GroupID") into g
select new
{
GroupID = g.Key,
SalesPerson = g.First(g2 => g2.Field<int>("Sales") == g.Min(g3 => g3.Field<int>("Sales"))).Field<string>("SalesPerson"),
Sales = g.Min(p => p.Field<int>("Sales"))
};
foreach (var r in result)
{
System.Console.WriteLine($"{r.GroupID},{r.SalesPerson} , {r.Sales}");
}
}
public static void Client2()
{
var data = GetData().AsEnumerable();
var result = data.GroupBy(t => t.Field<int>("GroupID")).Select(g => new
{
GroupID = g.Key,
SalesPerson = g.First(g2 => g2.Field<int>("Sales") == g.Min(g3 => g3.Field<int>("Sales"))).Field<string>("SalesPerson"),
Sales = g.Min(p => p.Field<int>("Sales"))
});
}
private static DataTable GetData()
{
DataTable table = new DataTable();
table.Columns.Add("GroupID", typeof(int));
table.Columns.Add("SalesPerson", typeof(string));
table.Columns.Add("Sales", typeof(int));
DataRow row = table.NewRow();
row["GroupID"] = 1;
row["SalesPerson"] = "Lee";
row["Sales"] = 900;
table.Rows.Add(row);
row = table.NewRow();
row["GroupID"] = 1;
row["SalesPerson"] = "Li";
row["Sales"] = 800;
table.Rows.Add(row);
row = table.NewRow();
row["GroupID"] = 1;
row["SalesPerson"] = "Park";
row["Sales"] = 990;
table.Rows.Add(row);
row = table.NewRow();
row["GroupID"] = 2;
row["SalesPerson"] = "Tom";
row["Sales"] = 500;
table.Rows.Add(row);
row = table.NewRow();
row["GroupID"] = 2;
row["SalesPerson"] = "Jane";
row["Sales"] = 400;
table.Rows.Add(row);
row = table.NewRow();
row["GroupID"] = 3;
row["SalesPerson"] = "Irina";
row["Sales"] = 600;
table.Rows.Add(row);
row = table.NewRow();
row["GroupID"] = 3;
row["SalesPerson"] = "Yong";
row["Sales"] = 633;
table.Rows.Add(row);
return table;
}
}
}
Loading…
Cancel
Save