我想在a中显示客户的会计历史记录DataGridView,我希望有一个列显示其余额的运行总计.我这样做的旧方法是获取数据,循环数据,逐行添加行DataGridView并计算当时的运行总数.瘸.我宁愿使用LINQ to SQL,或LINQ如果不能使用LINQ to SQL,来计算运行总数,这样我就可以设置DataGridView.DataSource为我的数据.
这是我正在拍摄的一个超级简化的例子.说我有以下课程.
class Item
{
public DateTime Date { get; set; }
public decimal Amount { get; set; }
public decimal RunningTotal { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想要一个可以生成如下结果的L2S或LINQ语句:
Date Amount RunningTotal
12-01-2009 5 5
12-02-2009 -5 0
12-02-2009 10 10
12-03-2009 5 15
12-04-2009 -15 0
Run Code Online (Sandbox Code Playgroud)
请注意,可能有多个具有相同日期的项目(12-02-2009).在计算运行总计之前,应按日期对结果进行排序.我猜这意味着我需要两个语句,一个用于获取数据并对其进行排序,另一个用于执行运行总计算.
我希望Aggregate能做到这一点,但它并不像我希望的那样有效.或者也许我只是想不出来.
这个问题似乎跟我想要的一样,但我不知道接受/唯一的答案是如何解决我的问题的.
关于如何解决这个问题的任何想法?
编辑 结合Alex和DOK的答案,这就是我最终得到的结果:
decimal runningTotal = 0;
var results = FetchDataFromDatabase()
.OrderBy(item => item.Date)
.Select(item => new …Run Code Online (Sandbox Code Playgroud) 假设我们在Windows应用程序中有以下代码:
ComboBox comboBox = new ComboBox()
{
AutoCompleteMode = AutoCompleteMode.SuggestAppend,
AutoCompleteSource = AutoCompleteSource.ListItems,
DataSource = new string[] { "", "Ark", "Boat", "Bucket" },
DropDownStyle = ComboBoxStyle.DropDownList
};
this.Controls.Add(comboBox);
TextBox textBox = new TextBox()
{
Left = comboBox.Right,
Top = comboBox.Top,
ReadOnly = true
};
textBox.DataBindings.Add("Text", comboBox, "SelectedValue");
this.Controls.Add(textBox);
Run Code Online (Sandbox Code Playgroud)
这里没有魔法,只是ComboBox绑定到一个字符串列表.该TextBox显示SelectedValue的ComboBox.
当我在" ComboBox标签"中输入"Bucket"时,我会遇到意外行为.由于某种原因,ComboBox显示"船"但TextBox显示"桶".我希望他们都能显示"Bucket".
如果我更改DropDownStyle为DropDown,它的行为与预期一致,但我不希望用户能够键入他们想要的任何内容.他们应该只能输入列表中的项目.
更有趣的是,在输入"Bucket"并跳出标签后,如果我再次输入"Bucket",它将同时显示"Bucket".如果我进行第三次尝试,它将返回"船",ComboBox而"Bucket"则返回"TextBox".所以看起来它正在骑自行车穿过所有的B.
在我最近从XP升级到Windows 7之前,我没有注意到这一点.我不明白这与此有什么关系,但无论如何我都把它丢掉了.
如果这种行为是正确的,有人能告诉我我应该做些什么来实现我预期的行为吗?
UPDATE 这似乎这是因为预计在Windows XP模式与Windows …
我正在尝试将一些旧的SQL重写为LINQ to SQL.我有一个带有GROUP BY WITH ROLLUP的sproc但是我不确定LINQ等价物是什么.LINQ有一个GroupBy,但看起来它不支持ROLLUP.
我试图获得的结果的简化示例将是这样的:
+-----------+---------------+--------------------+ | City | ServicePlan | NumberOfCustomers | +-----------+---------------+--------------------+ | Seattle | Plan A | 10 | | Seattle | Plan B | 5 | | Seattle | All | 15 | | Portland | Plan A | 20 | | Portland | Plan C | 10 | | Portland | All | 30 | | All | All | 45 | +-----------+---------------+--------------------+
关于如何使用LINQ to SQL获得这些结果的任何想法?
在我的功能文件中,IntelliSense表示有一个名为的关键字Scenarios.注意它是复数.我已经完成了文档,但找不到任何引用.任何人都可以解释它的用途以及如何使用它?
编辑:让我们再试一次.这次我使用了AdventureWorks示例数据库,所以你可以一起玩.这将排除我在自己的数据库中所做的任何疯狂.这是一个新的例子,展示了什么有用,我期望工作(但没有).任何人都可以解释为什么它不起作用或建议一种不同的方式来实现我的目标(重构共同的表达,以便它可以在其他地方重用)?
using (AdventureWorksDataContext db = new AdventureWorksDataContext())
{
// For simplicity's sake we'll just grab the first result.
// The result should have the name of the SubCategory and an array of Products with ListPrice greater than zero.
var result = db.ProductSubcategories.Select(subCategory => new
{
Name = subCategory.Name,
ProductArray = subCategory.Products.Where(product => product.ListPrice > 0).ToArray()
}).First();
Console.WriteLine("There are {0} products in SubCategory {1} with ListPrice > 0.", result.ProductArray.Length, result.Name);
// Output should say: There are 3 products in SubCategory …Run Code Online (Sandbox Code Playgroud) 我有一个方法,目前Func<Product, string>作为一个参数,但我需要它作为一个Expression<Func<Product, string>>.使用AdventureWorks,这是我想用Func做的一个例子.
private static void DoSomethingWithFunc(Func<Product, string> myFunc)
{
using (AdventureWorksDataContext db = new AdventureWorksDataContext())
{
var result = db.Products.GroupBy(product => new
{
SubCategoryName = myFunc(product),
ProductNumber = product.ProductNumber
});
}
}
Run Code Online (Sandbox Code Playgroud)
我希望它看起来像这样:
private static void DoSomethingWithExpression(Expression<Func<Product, string>> myExpression)
{
using (AdventureWorksDataContext db = new AdventureWorksDataContext())
{
var result = db.Products.GroupBy(product => new
{
SubCategoryName = myExpression(product),
ProductNumber = product.ProductNumber
});
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我遇到的问题myExpression(product)是无效(不会编译).阅读其他一些帖子后,我理解为什么.如果不是因为我需要product变量用于我的密钥的第二部分,我可能会说这样的事情:
var result = db.Products.GroupBy(myExpression);
Run Code Online (Sandbox Code Playgroud)
但我确实需要product变量,因为我确实需要密钥的第二部分(ProductNumber).所以我现在不确定要做什么.我不能把它当成Func,因为这会导致问题.我无法弄清楚如何使用Expression,因为我没有看到如何将 …
我有一个CheckBox,当选中/取消选中时,将切换Enabled其他一些控件的属性.我确实让我的代码看起来像这样:
checkBox.CheckedChanged += new EventHandler((o, e) =>
{
control1.Enabled = checkBox.Checked;
control2.Enabled = checkBox.Checked;
});
Run Code Online (Sandbox Code Playgroud)
但今天我开始玩,DataBindings发现我可以这样做:
control1.DataBindings.Add("Enabled", checkBox, "Checked");
control2.DataBindings.Add("Enabled", checkBox, "Checked");
Run Code Online (Sandbox Code Playgroud)
他们似乎行为相同,但我怀疑一个比另一个更受欢迎.或者也许有人会有一些意想不到的行为可能会让我后来吵架.
有一种方式比另一种更好吗?
DataGridView.IsCurrentRowDirtytrue在我提交数据库更改后仍然存在.我想将它设置为当它失去焦点时它false不会触发RowValidating.
我有一个DataGridView必然的BindingList<T>.我处理CellEndEdit事件并保存对数据库的更改.保存这些更改之后我想DataGridView.IsCurrentRowDirty设置true,因为该行中的所有单元格都是最新的; 然而,它被设定为false.
这会给我带来问题,因为当行失去焦点时它会触发RowValidating,我会处理并验证所有三个单元格.所以即使所有单元格都有效且没有一个是脏的,它仍然会验证它们.那是浪费.
这是我的一个例子:
void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
// Ignore cell if it's not dirty
if (dataGridView.isCurrentCellDirty)
return;
// Validate current cell.
}
void dataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
// Ignore Row if it's not dirty
if (!dataGridView.IsCurrentRowDirty)
return;
// Validate all cells in the current row.
}
void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// Validate all …Run Code Online (Sandbox Code Playgroud) 我希望有一个TextBox支持自动完成并允许用户输入以逗号或分号分隔的多个单词,并为每个单词提供建议的工具。我有一个TextBox标准
textBox.AutoCompleteCustomSource.AddRange(new[] { "apple", "banana", "carrot" });
textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
Run Code Online (Sandbox Code Playgroud)
不幸的是,它只会建议第一个单词。此后输入任何内容,它就会停止提示。
我希望能够执行以下场景:
我尝试过谷歌搜索寻找解决方案,但运气不佳。这似乎是 Web 应用程序的一个流行功能,但显然不适用于 winform。有什么建议么?
我试图从文本文件中获取数千张发票(和其他东西)并将它们插入SQL Server 2008.我写了一个小控制台应用程序来执行此操作并使用LINQ to SQL.在我插入所有现有发票之后,我希望它Invoice_ID是一个标识列并自动增加,所以我将它设计为:
CREATE TABLE [dbo].[Invoice]
(
[Invoice_ID] int IDENTITY(1,1) NOT NULL PRIMARY KEY,
/* Other fields elided */
)
Run Code Online (Sandbox Code Playgroud)
在我开始插入发票之前,我调用包含以下行的存储过程:
SET IDENTITY_INSERT [dbo].[Invoice] ON;
/* Other lines for setup elided */
Run Code Online (Sandbox Code Playgroud)
在我提交更改后,我使用以下行调用另一个存储过程:
SET IDENTITY_INSERT [dbo].[Invoice] OFF;
/* Other lines for clean up elided */
Run Code Online (Sandbox Code Playgroud)
当我尝试插入发票并提交更改时,我收到以下异常:
SQLException:当IDENTITY_INSERT设置为OFF时,无法在表'Invoice'中为identity列插入显式值.
我运行SQL事件探查器,我可以看到它确实设置IDENTITY_INSERT为ON尝试执行插入之前.我不认为它被设置在OFF任何地方.我是SQL Profiler的新手,所以也许我可以启用一个事件,它将为我提供更多信息来尝试和调试它.有任何想法吗?
我已经尝试调整LINQ to SQL使用的.dbml文件中的值.我将Invoice表Auto Generated Value设置为"False",Auto-Sync设置为"Never",Server Data Type设置为"Int NOT NULL".通常我会将它们分别设置为"True","On Insert"和"Int NOT NULL IDENTITY",但是当我这样做时,我可以看到SQL …
c# ×7
.net ×6
linq-to-sql ×5
winforms ×4
data-binding ×2
linq ×2
autocomplete ×1
combobox ×1
datagridview ×1
identity ×1
specflow ×1
textbox ×1
validation ×1
windows-7 ×1