2012-01-26 14 views
7

mam typu enum nazwie StatusTypesokreślonym Państwie typu nie jest obsługiwana w LINQ do podmiotów

public enum StatusTypes 
{ 
    Open = 1, 
    Allocated = 2, 
    WorkInProgress = 3, 
    WaitingOnRequestor = 4, 
    WaitingOnThirdParty = 5, 
    Monitoring = 6, 
    Testing = 7, 
    OnHold = 8, 
    Complete = 9, 
    SignedOff = 10, 
    Reopened = 11 
} 

Próbuję użyć tego w moim repozytorium ....

public IQueryable<Incident> GetAllOutstandingIncidents() 
{ 
    return from i in db.Incidents 
       where i.Status != Types.StatusTypes.SignedOff && i.Status != Types.StatusTypes.Complete && i.DeletedDateTime != null 
       orderby i.DueDateTime 
       select i; 
    } 

... a następnie używać go w moim zdaniem ...

<tbody> 
    <% foreach (var incident in Model.TotalIncidentsOutstandingList) { %> 
       <tr> 
        <td><%: incident.IncidentID %></td> 
        <td><%: incident.Caller.NetworkName %></td> 
        <td><%: incident.Title %></td> 
        <td><%: incident.Service.Title %>/<%: incident.Category.Title %> <% if (incident.Subcategory != null) { %>/<%: incident.Subcategory.Title %><% } %></td> 
        <td><%: incident.Priority %></td> 
        <td></td> 
        <td><%: incident.AllocatedTo %></td> 
        <td><%: incident.DueDateTime %></td> 
       </tr> 
      <% } %> 
     </tbody> 

... ale dostaję błąd „Podana członkiem typu«Stan»nie jest obsługiwana w LINQ do Ent to. Tylko inicjalizatory, członkowie jednostki, a jednostka nawigacji właściwości są obsługiwane.”

Każda pomoc z wdzięcznością otrzymał!

UPDATE DO SHOW incident.cs

public class Incident 
{ 
    public int IncidentID { get; set; } 
    public DomainUser Caller { get; set; } 

    [Display(Name = "Caller Type")] 
    public Types.CallerTypes CallerType { get; set; } 

    public Service Service { get; set; } 
    public Category Category { get; set; } 
    public Subcategory Subcategory { get; set; } 
    public string Title { get; set; } 

    [Display(Name = "Problem Description")] 
    public string ProblemDescription { get; set; } 

    public Equipment Equipment { get; set; } 

    public Types.ImpactTypes Impact { get; set; } 
    public Types.UrgencyTypes Urgency { get; set; } 

    [Display(Name = "Priority")] 
    public Types.PriorityTypes Priority { get; set; } 

    [Display(Name="Estimated time for completion")] 
    public DateTime? DueDateTime { get; set; } 

    [Display(Name="Date/Time")] 
    public DateTime? CreatedDateTime { get; set; } 
    public DomainUser CreatedBy { get; set; } 

    [Display(Name = "Allocated To")] 
    public HelpDeskMember AllocatedTo { get; set; } 
    public DateTime? AllocatedDateTime { get; set; } 

    public DateTime? ClosedDateTime { get; set; } 
    public int? ClosedBy { get; set; } 

    public DateTime? ReopenedDateTime { get; set; } 
    public int? ReopenedBy { get; set; } 

    public DateTime? DeletedDateTime { get; set; } 
    public HelpDeskMember DeletedBy { get; set; } 

    public Decimal? EstimatedInternalCost { get; set; } 
    public Decimal? EstimatedResources { get; set; } 
    public Decimal? RealInternalCost { get; set; } 
    public Decimal? EstimatedExternalCost { get; set; } 
    public Decimal? RealExternalCost { get; set; } 
    public Decimal? EstimatedTotalCost { get; set; } 
    public Decimal? RealTotalCost { get; set; } 

    public string CostCode { get; set; } 

    public string TimeRequired { get; set; } 
    public string ActualTimeTaken { get; set; } 

    public Types.StatusTypes Status { get; set; } 

    public string Solution { get; set; } 

    public bool UserSignedOff { get; set; } 

    public bool OverdueEmailSent { get; set; } 
    public bool EscalatedEmailSent { get; set; } 

    public ICollection<Note> Notes { get; set; } 
    public ICollection<Attachment> Attachments { get; set; } 
    public ICollection<HistoryItem> History { get; set; } 

    public Incident() 
    { 
     Notes = new List<Note>(); 
     Attachments = new List<Attachment>(); 
     History = new List<HistoryItem>(); 
    } 
} 
+0

Jaki typ ma własność 'Status'? Jeśli jest to 'int', dostaniesz błąd podczas kompilacji. Jeśli jest to 'enum', musisz rzucić obie części warunku równości do' int' – WarHog

+0

Hi @WarHog, właściwość 'Status' ma typ' Types.StatusTypes', który jest typem 'enum'. Czy mógłbyś podać przykład, jak rzucać obie części do 'int', proszę? Dzięki. –

+0

Czy możemy zobaczyć twoją klasę wypadków? – NinjaNye

Odpowiedz

6

Jak już powiedziałem, spróbuj odrzucić obie części do int typ

public IQueryable<Incident> GetAllOutstandingIncidents() 
{ 
    return from i in db.Incidents 
     where (int)i.Status != (int)Types.StatusTypes.SignedOff 
      && (int)i.Status != (int)Types.StatusTypes.Complete 
      && i.DeletedDateTime != null 
     orderby i.DueDateTime 
     select i; 
} 

UPDATE

To cecha kod najpierw. Powinieneś wykonać następujące czynności. Zmień swoją klasę tak:

[Column("Status", TypeName = "int")] 
public int InternalStatus { get; set; } 
public StatusTypes Status { get; set; } 

I użyć następującego zapytania:

context.Incidents.Where(i => i.InternalStatus == (int)StatusTypes.Allocated); 

Znalazłem ten informacje here

+0

Dzięki To niestety nie rozwiązuje tego błędu: –

+0

Istnieje rozwiązanie - zaktualizowałem swój post – WarHog

+0

Wygląda na to, że udało mi się to zrobić .. –

1

Spróbuj zamiast

return from i in db.Incidents 
       where i.Status != (int)Types.StatusTypes.SignedOff && i.Status != (int)Types.StatusTypes.Complete && i.DeletedDateTime != null 
       orderby i.DueDateTime 
       select i; 
+0

Witam @ Greco, dzięki za odpowiedź. Próbowałem już, i mam "Operator"! = "Nie można zastosować do operandów typu" Cognito.Models.Types.StatusTypes "i" int "" –

+0

oh, a następnie spróbuj wykonać następujące czynności: int signedOff = (int) Types.StatusTypes.SignedOff; int complete = (int) Types.StatusTypes.Complete; a następnie użyj zmiennych lokalnych w zapytaniu –

1

W swojej klasie zdarzenia:

private int statusId; 
public Types.StatusTypes Status 
{ 
    get 
    { 
     return (Types.StatusTypes)statusId; 
    } 
    set 
    { 
     statusId = (int)value; 
    } 
} 

public Int StatusId 
{ 
    get 
    { 
     return statusId; 
    } 
} 

Następnie w was metoda:

public IQueryable<Incident> GetAllOutstandingIncidents() 
{ 
    int signedOffStatusType = (int)Types.StatusTypes.SignedOff;  
    int completeStatusType = (int)Types.StatusTypes.Complete; 

    return from i in db.Incidents 
      where i.StatusId != signedOffStatusType 
       && i.StatusId != completeStatusType 
       && i.DeletedDateTime != null 
     orderby i.DueDateTime 
     select i; 
} 

lub za pomocą metody składni:

public IQueryable<Incident> GetAllOutstandingIncidents() 
{ 
    int signedOffStatusType = (int)Types.StatusTypes.SignedOff;  
    int completeStatusType = (int)Types.StatusTypes.Complete; 

    return db.Incidents.Where(i => i.StatusId != signedOffStatusType 
           && i.StatusId != completeStatusType 
           && i.DeletedDateTime != null) 
         .OrderBy(i => i.DueDateTime); 
} 
+0

Dziękuję, próbowałem, nadal nie mam szczęścia, obawiam się! –

+0

Czy najpierw używasz kodu? Czy możemy zobaczyć twoją klasę wypadków ... ah właśnie to widzieliśmy, ta – NinjaNye

+0

Tak, używając najpierw EF Code, po prostu napisałeś ... –

4

Można również konwertować do LINQ do obiektu z .AsEnumerable():

public IQueryable<Incident> GetAllOutstandingIncidents() 
{ 
    return from i in db.Incidents.AsEnumerable() 
       where i.Status != Types.StatusTypes.SignedOff && i.Status != Types.StatusTypes.Complete && i.DeletedDateTime != null 
       orderby i.DueDateTime 
       select i; 
} 

W zależności od tego, co chcesz, to może być nie tak dobre rozwiązanie: Zamiast ciągnąć jeden obiekt z bazy danych, będzie on ciągnął wszystkie obiekty, gdy wywołasz .AsEnumerable().

Powiązane problemy