2017-02-22 2 views
0

Mit Entity-Framework versuche ich, Datensätze und ihre zugehörigen untergeordneten Datensätze auszuwählen. Die untergeordneten Datensätze könnten null sein, daher möchte ich nur eine leere Zeichenfolge zurückgeben, wenn sie das sind. Ich bin eineLinq-Abfrage mit null untergeordneten Entitäten

null getting Ausnahme

beim Versuch, den folgenden Code auszuführen.

var query = ctx.UserAlerts.Include(x=>x.DocumentType).Where(x => x.UserId == userId).ToList(); 
var alerts = query.Select(u => new AlertConfigVM 
      { 
       UserId = u.UserId, 
       Destination = u.AlertDestination, 
       TypeofAlert = u.TypeofAlert, 
       HourInterval = u.IntervalAsHours, 
       DocumentTypeName= u.DocumentType.Name??string.Empty 
      }).ToList(); 

hier sind meine Entities

public class UserAlert 
{ 
    public int Id { get; set; } 
    public string UserId { get; set; } 
    public User User { get; set; } 
    public int TypeofAlert { get; set; } 
    public string AlertDestination { get; set; } 
    public int? DocumentTypeId { get; set; } 
    public virtual DocumentType DocumentType { get; set; } 
    public int? IntervalAsHours { get; set; } 
} 

public class DocumentType 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Key { get; set; } 
} 

und hier mein Rückgabetyp ist. diese

public class AlertConfigVM 
{ 
    public int Id { get; set; } 
    public string UserId { get; set; } 
    public User User { get; set; } 
    public int TypeofAlert { get; set; } 
    public string Destination { get; set; } 
    public int? DocumentTypeId { get; set; } 
    public string DocumentTypeName { get; set; } 
    public int? HourInterval { get; set; } 
} 
+0

Stellen Sie sicher, dass documentType nicht null ist, bevor Sie prüfen, ob dokumenttyp.name nicht null ist. – Forklift

+0

wie würde ich das tun? Können Sie ein Beispiel veröffentlichen? –

+1

Mögliches Duplikat von [Was ist eine NullReferenceException, und wie behebe ich sie?] (Http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –

Antwort

3

Wechsel:

DocumentTypeName= u.DocumentType.Name??string.Empty 

dazu:

DocumentTypeName= u.DocumentType?.Name??string.Empty 

Dies ermöglicht es dem Document zu string.Empty wenn null auf Standard.

+0

Super, das hat perfekt funktioniert. Ich wusste nicht, dass du das tun könntest. Wo hast du diesen handlichen kleinen Trick aufgeschnappt? –

+0

Ehrlich gesagt, habe ich es "zufällig" mit ReSharper entdeckt, wenn ich ehrlich bin. – tjcertified

+2

Beachten Sie, dass diese Lösung NUR ** in Visual Studio 2015 und später mit MSBuild unter Verwendung von Roslyn funktioniert. – Cameron

1

Seit tjcertified's answer funktioniert nur in Visual Studio 2015, und diese Lösung sollte später, arbeitet unabhängig von Umwelt:

var query = ctx.UserAlerts.Include(x=>x.DocumentType).Where(x => x.UserId == userId).ToList(); 
var alerts = query.Select(u => new AlertConfigVM 
      { 
       UserId = u.UserId, 
       Destination = u.AlertDestination, 
       TypeofAlert = u.TypeofAlert, 
       HourInterval = u.IntervalAsHours, 
       DocumentTypeName= u.DocumentType != null 
            ? u.DocumentType.Name != null 
             ? u.DocumentType.Name 
             : string.Empty 
            : string.Empty 
      }).ToList(); 

Der ?: Betrieb die ternary operator und ist das gleiche wie eine reguläre if-else Aussage.

+3

Sie könnten es kürzer machen: ('u.DocumentType == null? Null: u.DocumentType.Name) ?? string.Empty' –

+0

@AndersonPimentel Schöner Fang. Daran habe ich nicht gedacht. – Cameron

Verwandte Themen