草庐IT

null_object

全部标签

c# - SingleOrDefault 返回 null 时的 LINQ 新实例

我有这个简单的方法:#regionFieldsprivateCollection_addresses;#endregion#regionPublicmethodspublicAddressDeliveryAddress(){if(_addresses==null)if(this.Id>0)_addresses=Core.Data.Addresses.GetClient(this.Id);return_addresses.SingleOrDefault(x=>x.TypeId==AddressType.Delivery);}publicAddressInvoiceAddress(){if

c# - 如果在 linq 查询中值为 null,如何分配空字符串?

我有以下LINQ查询来获取一组数据。varfields=fromrowindatarowsfromfieldinrowfromcolincolumnnameswherefield.Key==colselectnew{ColumnName=col,FieldValue=field.Value};问题是我处理此查询后字段的代码失败,因为某些行的field.Value返回null。我的目标是在检测到null时分配一个空字符串。类似iffield.Value==null,thenfield.Value=""是否可以在linq查询中这样做? 最佳答案

c# - 检查 single() LINQ 是否返回 NULL

我有一个LINQ查询,它应该返回单个结果或不返回结果。我调用Single()得到这样的结果:varpropertyDataSource=(fromxinmyCollectionwhereSomeCondition(x)selectx).Single();当我的查询只有一个结果时,这可以正常工作,但如果没有结果,它会抛出System.InvalidOperationException消息Sequencecontainsnoelements。p>我该如何解决这个问题? 最佳答案 使用SingleOrDefault相反。Single当枚举

c# - 我想要 "(int)null"返回我 0

如何从(int)null中获取0作为整数值。编辑1:我想创建一个函数,该函数将返回各自数据类型中null表示的默认值。编辑2:我如何在此场景中使用默认。(整数)值其中Value可以是null或任何整数值。我不知道运行时值的数据类型。但我会保证Value应该只包含null或Integer值。 最佳答案 您可以使用default关键字获取任何数据类型的默认值:intx=default(int);//==0stringy=default(string);//==null//etc.这也适用于通用参数:BarFoo(){returnnewB

c# - ASP.NET Web API Authentication.GetExternalLoginInfoAsync 总是返回 null

我有ASP.NET5项目,我正在使用WebAPI建立外部登录(用于Facebook和Google)。就我而言,我有包含以下代码的WebAPIController(不是MVCController):[OverrideAuthentication][HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)][AllowAnonymous][Route("ExternalLogin",Name="ExternalLogin")]publicasyncTaskGetExternalLogin(stringprovider,str

c# - EF 5 代码迁移错误 : "There is already an object named _____ in the database"

在进行EF5代码迁移时遇到了一个反复出现的奇怪问题,现在让我无法工作。尝试运行update-database并收到此错误:Thereisalreadyanobjectnamed'RequestStatus'inthedatabase.详细的日志转储:PM>update-database-vUsingStartUpproject'LicensingWorkflow'.UsingNuGetproject'LicensingWorkflow'.Specifythe'-Verbose'flagtoviewtheSQLstatementsbeingappliedtothetargetdataba

c# - UWP VisualTreeHelper.GetParent() 返回 null

我有一个ContentDialog,它有一个ListView。此ListView的DataTemplate包含一个Grid,此Grid有一个Button。代码是这样的:......这是我的ButtonClick事件处理程序:privateasyncvoidButton_Click(objectsender,RoutedEventArgse){vargrid=VisualTreeHelper.GetParent(senderasButton)asGrid;......}问题是变量VisualTreeHelper.GetParent(senderasButton)asGrid在我的PC上总

c# - LinqToSQL 错误 : Operation is not valid due to the current state of the object

在更新命令期间,我收到以下错误:Operationisnotvalidduetothecurrentstateoftheobject我试图从更新命令中删除一列并且它工作正常。此列是一个FK,与其他工作正常的FK相似。这是执行更新的代码:ti.NumeroTitolo=titolo.Numero;ti.RKTipoTitoloGenereTitolo=titolo.RkTipoTitoloGenereTitolo;ti.RKBanca=titolo.RkBanca;ti.DataScadenza=titolo.DataScadenza;ti.RKTipoEsito=titolo.RkTi

c# - 在 C# 中,Type.FullName 何时返回 null?

MSDNforType.FullName说这个属性返回nullifthecurrentinstancerepresentsagenerictypeparameter,anarraytype,pointertype,orbyreftypebasedonatypeparameter,oragenerictypethatisnotagenerictypedefinitionbutcontainsunresolvedtypeparameters.我数了五种情况,发现一个比一个更不清楚。这是我尝试构建每个案例的示例。usingSystem;usingSystem.Collections.Gene

c# - Entity Framework 6 : Adding child object to parent's list vs. 将子项的导航属性设置为父项

我有一个现有数据库,其中包含两个表MailServers和MailDomains。MailDomains的外键列MailServerId指向MailServers中的Id主键列。所以我们这里是一对多的关系。我关注了thisarticle并通过实体数据模型向导中的“代码优先从数据库”模型创建了我的EntityFrameworkPOCO。这产生了以下两个C#类:publicpartialclassMailServer{publicMailServer(){MailDomains=newHashSet();}publicintId{get;set;}publicvirtualICollect