我有一张米的桌子。然后是一个Meter读数表(它引用了Meter表,以及一个Date列和一个读数值列)。这个想法是,对于每一天,一个人将记录给定仪表的读数。UI运行良好,我可以选择一个仪表,然后单击按钮添加一个新的读数,然后在读数网格中添加一个空白的“读数”行。读数的输入日期默认为今天。
我想要做的是将读数日期默认为特定仪表的上次读数日期+1天。我设想的是,在Reading_Created处理程序中,我有如下伪代码:
var lastDate = DataWorkspace.Data.Readings
.Where(r=> r.MeterID == this.MeterID)
.Max(r=> r.ReadingDate);
this.ReadingDate = lastDate.AddDays(1);这在Lightswitch应用程序中是可能的吗?
发布于 2012-08-14 09:56:40
如果您使用this.ReadingCollection.AddNew()添加新的读数,那么新添加的读数将正确设置其父仪表。
由于Meter和它的读数之间存在关系,您可以通过将代码修改为如下所示来利用这种关系:
partial void Reading_Created()
{
//get parent meter's existing Readings
var previousReadings = (from r in this.Meter.Readings select r)
//if previous readings exist for this meter, get the last date, & add a day to it
if (previousReadings.Any())
{
this.ReadingDate = previousReadings.Max(d => d.ReadingDate).AddDays(1);
}
//otherwise, just use today's date
else
{
this.ReadingDate = Date.Today();
}
}这样做,您不需要过滤Readings表的记录(关系会为您做这件事),您不需要对它们进行排序&您不需要TakeOne (如果没有记录,这将失败)。
发布于 2012-08-14 04:38:38
您可以将此代码添加到实体的Created()事件中:
partial void Readings_Created()
{
ReadingDate = (from o in this.DataWorkspace.Data.Readings
where MeterID == this.MeterID
orderby o.ReadingDate descending
select o).Take(1).Execute().Max(o => o.ReadingDate).AddDays(1);
}我测试了一组类似的代码,它计算出新行的正确日期。如果没有MeterID的条目,我没有测试这是否可以工作。
发布于 2014-04-05 20:28:03
I know this post is older but for anyone researching the method, thanks to Beth Massi vid HDI#20 I came up with this for a similar screen./This方法在单击绿色+按钮时激发,它查看集合中的选定项,并将信息从选定项复制到要添加的新项中。/
partial void Worklists_Changed(NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
if (this.Worklists.Count > 1 & e.NewItems.Count == 1)
{
try
{
Worklist newRecord = (Worklist)e.NewItems[0];
Worklist currentRecord = this.Worklists.SelectedItem;
newRecord.StartTime = currentRecord.StartTime.Value.AddDays(1);
newRecord.EndTime = currentRecord.EndTime.Value.AddDays(1);
newRecord.WorklistCode = currentRecord.WorklistCode;
}
catch (Exception ex)
{
Trace.TraceInformation("Could not copy data into new row " + ex.ToString());
}
}
}
}https://stackoverflow.com/questions/11937712
复制相似问题