2016-08-18 49 views
0

我對使用MVC和LINQ理解某些數據類型和數據集有困難。我正在嘗試填充下拉列表。populate dropdownlist for mvc

我收到以下錯誤(在下面的代碼說明)

「無法創建類型‘vps_intranet.Models.Part’的恆定值,只有原始類型或枚舉類型在支持這個背景「。

PartsController.cs

private List<Part> partsNotPartOfStructure(int partID) 
{ 
    Part mainPart = db.Parts.Find(partID); 
    //var alreadySelected = db.Parts.Select(p => p.PartStructures_comp).Distinct(); 
    List<Part> parts = new List<Part>(); 

    List<PartStructures> excludeList = db.PartStructures1 
     .Where(p => p.mainPart_id == partID).ToList(); 

    parts = db.Parts.Where(c => c.PartStructures_comp 
     .Except((List<PartStructures>) excludeList)).ToList(); 
    //The line above gives the error... 
    //Unable to create a constant value of type 'vps_intranet.Models.Part'. 
    //Only primitive types or enumeration types are supported in this context.** 

    return parts; 
} 

public async Task<ActionResult> Details(int? id) 
{ 
    if (id == null) 
    { 
     return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
    } 
    Part part = await db.Parts.FindAsync(id); 
    if (part == null) 
    { 
     return HttpNotFound(); 
    } 
    ViewData["AvailableParts"] = partsNotPartOfStructure(id.Value); 
    return View(part); 
} 

Details.cshtml

@model vps_intranet.Models.Part 

@{ 
    var fullList = (IEnumerable<vps_intranet.Models.Part>) ViewData["AvailableParts"]; 
    var availableParts = fullList.Select(p => new SelectListItem { Text = p.partNo.ToString(), Value = p.id }); 
} 

... 

@Html.DropDownListFor(model => model.PartStructures_comp, availableParts)); 

我需要做什麼改變嗎?

+1

確實[這](http://stackoverflow.com/questions/18929483/unable-to-create-a-constant-value-of-type -only-primitive-types-or-enumeration-ty)有幫助嗎? –

+1

您正嘗試在兩種不同的數據類型之間使用「排除」。你有'Part'類,然後是'List '。按部件進行。首先,獲取零件。然後,將其轉換爲零件結構列表,然後執行排除 –

+1

您可能想查看[ASP.Net MVC中的DropDownList](http://stackoverflow.com/questions/37818949/best-programming-practice-of -using-dropdownlist-in-asp-net-mvc/37819577#37819577) – Win

回答

1

您正在將類對象列表PartStructures傳遞給Except方法。除了方法使用默認的相等比較器來比較值。

如果你傳遞一個自定義類(不像int一個簡單的值類型),你應該實現IEqualityComparer<T>接口中的方法,如EqualsGetHashCode

如果您不喜歡這樣做,您可以獲取PartStructures集合的ID並將其與Contains方法一起使用。

var excludeIdList = db.PartStructures1.Where(p => p.mainPart_id == partID) 
         .Select(g=>g.Id).ToList(); 

var parts = db.Parts 
      .Where(c => !c.PartStructures_comp.Any(g=>excludeIdList.Contains(g.Id))) 
      .ToList(); 
+0

感謝您的幫助。我更瞭解我做錯了什麼。但是,結果是排除列表中的部分,而不是列表的其餘部分。我嘗試使用除了但我得到一個錯誤。顯然我有點學習。你將如何實施Except條款來代替包含? – Beengie

+0

正如我在答案中解釋的那樣,如果你想使用Except,你需要實現'Equals'和'GetHashCode'方法。我發佈的解決方案有什麼問題?它應該爲您提供所有沒有PartStructures_comp的部分,其中Ids與exlucdeIdList變量中的id相同。 – Shyju

+0

我在添加「」== false「之前實現了您的修復程序,並返回了包含的記錄。」== false「後,它不返回任何記錄 – Beengie

0

您試圖在兩種不同的數據類型之間使用Exclude。你有Part課,然後是List<PartStructure>。按部件進行。首先,獲取零件。然後,將其轉換爲零件結構列表,然後執行排除。

您必須實現IEqualityComparer<T>

來自this崗位。

public class Compare : IEqualityComparer<Part> 
{ 
    public bool Equals(Part x, Part y) 
    { 
     return x.SomeProperty == y.SomeProperty; 
    } 
    public int GetHashCode(Part part) 
    { 
     return part.SomeProperty.GetHashCode(); 
    } 
} 

這種方式,你可以做

var parts = db.Parts.Exclude(someList, new Compare());