FirstOrDefault“锁定在内部" * _InitializeDataWorkspace [英] FirstOrDefault "locked inside" *_InitializeDataWorkspace

查看:79
本文介绍了FirstOrDefault“锁定在内部" * _InitializeDataWorkspace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的场景,我无法运行".FirstOrDefault"屏幕中的* _InitializeDataWorkspace方法.

I have a strange scenario where I can't run a ".FirstOrDefault" inside a screen *_InitializeDataWorkspace method.

当我添加断点时,实体列表/表似乎被锁定/处于待命状态,直到打开了另一个可以从数据库中成功检索项目的屏幕.

When I add breakpoints it seems like the entity list/table is locked/placed on hold, untill another screen was opened that would successfully retrieve items from the database.

有什么想法吗?将不胜感激.

Any ideas? Would be much appreciated.

partial void CreateNewPerson_InitializeDataWorkspace(List<IDataService> saveChangesTo)
{
    //Code gets "locked" here:
    City city = this.DataWorkspace.ApplicationData.Cities.FirstOrDefault();
    
    //And proceed to this only when I open a different screen that load data from the database sucessfully
    if (city != null)
    {
        PersonProperty.City = city;
    }
}


推荐答案

我从来没有遇到过这种情况,&我在 InitializeDataWorkspace中初始化属性 始终保持这种方法.

I've never had this happen to me, & I initialise properties in the InitializeDataWorkspace method all the time.

不过有几点评论:

  • 您意识到, FirstOrDefault 这样使用,没有 lambda表达式,它将返回表中的第一条记录;如果不存在,则返回null,对吗?可能是碰巧首先添加到表中的任何记录.
  • 如果我知道要初始化的记录的ID,我倾向于使用 SingleOrDefault . (使用简单的lambda表达式" x => x.ID = myKnownIdValue ").
  • 这几天,我实际上添加了 query参数到屏幕上以表示我想参考的任何实体集合,用那个代替 DataWorkspace.DataSourceName .通过将查询添加到屏幕,LightSwitch现在知道"消息.关于它的& ;,将其包括在幕后的任何东西"中.它做到了.当然,它可能根本没有优势,但这就是我开始做的事情. 因此,您之前的代码将具有" this.queryParamaterName.FirstOrDefault(x => x.ID == aKnownIdValue)".
  • You realise that FirstOrDefault, used like that, with no lambda expression, is going to return the first record in the table, or null if none exist, right? It could be any record that happened to be added to the table first.
  • I tend to use SingleOrDefault, if I know the ID of the record I want to initialise with (with a simple lambda expression "x => x.ID = myKnownIdValue").
  • These days I add actually a query parameter to the screen to represent any entity collection I want to reference, & use that instead of using DataWorkspace.DataSourceName. By adding the query to the screen, LightSwitch now "knows" about it &, includes it in whatever behind the scenes "stuff" that it does. Of course it may provide no advantage at all, but it's what I've started doing. So your code earlier would have "this.queryParamaterName.FirstOrDefault(x => x.ID == aKnownIdValue)".

我希望对您有帮助?


这篇关于FirstOrDefault“锁定在内部" * _InitializeDataWorkspace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆