我有一个linq查询
var x = (from t in types select t).GroupBy(g =>g.Type)
Run Code Online (Sandbox Code Playgroud)
它按类型对对象进行分组,因此我想要包含所有分组对象及其计数的单个新对象.像这样的东西:
type1, 30
type2, 43
type3, 72
Run Code Online (Sandbox Code Playgroud)
更清楚:分组结果应该在一个对象中,而不是每个项目类型的对象
我正在研究一种绘图程序,但是在绘制橡皮带线时移动鼠标光标时出现闪烁问题.我希望你能帮助我删除那条闪烁的行,这里是代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GraphicsTest
{
public partial class Form1 : Form
{
int xFirst, yFirst;
Bitmap bm = new Bitmap(1000, 1000);
Graphics bmG;
Graphics xG;
Pen pen = new Pen(Color.Black, 1);
bool draw = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bmG = Graphics.FromImage(bm);
xG = this.CreateGraphics();
bmG.Clear(Color.White);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
xFirst = e.X; …
Run Code Online (Sandbox Code Playgroud) 我有一个巨大的开关/案例声明,我想用策略模式替换.每个语句都具有很强的代码执行特定逻辑.有没有人在这种情况下有一个很好的使用模式的例子,或者你有任何其他好的解决方案吗?
我的解决方案很好
class Context
{
private readonly List<CalculationUnit> _calculationsUnits;
public Context()
{
_calculationsUnits = new List<CalculationUnit>()
{
new CalculationUnitA("calc1"),
new CalculationUnitB("calc2"),
new CalculationUnitC("calc2")
};
}
public int Calculate(string name)
{
return (from c in _calculationsUnits where c.Name.Equals(name) select c.Calculate()).FirstOrDefault();
}
}
class CalculationUnit
{
public string Name { get; private set; }
public CalculationUnit(string name)
{
Name = name;
}
public virtual int Calculate()
{
return 0;
}
}
class CalculationUnitA : CalculationUnit
{
public CalculationUnitA(string name) : base(name) { …
Run Code Online (Sandbox Code Playgroud)