LINQ to Entities中不支持指定的类型成员

时间:2012-01-26 12:31:31

标签: c# asp.net-mvc-3 enums linq-to-entities

我有一个名为StatusTypes的枚举类型

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
}

我正在尝试在我的存储库中使用它....

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;
    }

...然后在我看来使用它......

<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>

...但我收到错误“LINQ to Entities不支持指定的类型成员'状态'。仅支持初始值设定项,实体成员和实体导航属性。”

感激不尽的任何帮助!

更新以显示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>();
    }
}

4 个答案:

答案 0 :(得分:6)

正如我已经说过的那样,尝试将两个部分都转换为int类型

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;
}

<强>更新

这是Code First的一个功能。你应该做以下。改变你的课程:

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

并使用以下查询:

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

我发现了这个信息here

答案 1 :(得分:5)

您还可以使用.AsEnumerable()转换为LINQ To Object:

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;
}

根据你的需要,它可能是一个不太好的解决方案:它不是从数据库中拉出一个对象,而是在你调用时拉取所有对象.AsEnumerable()。

答案 2 :(得分:1)

试试这个

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;

答案 3 :(得分:1)

在您的事件类中:

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

public Int StatusId 
{ 
    get 
    {
        return statusId;
    }
}

然后在你的方法:

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;
}

或者使用方法语法:

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);
}