2010-12-14 54 views
0

我想用Linq創建一個本身包含2個數據對象的對象。從Linq表達式中,我試圖直接設置對象,而不是在包含類中包含inbetween函數。Linq使用MVC的許多對象

含類看起來是這樣的:

public class SupplierCategories 
{ 

    public category _category; 
    public category_sub _category_sub; 
    private bool _IsActive; 
    private int? _SupplierID; 

    public SupplierCategories() 
    { 

    } 

    public bool IsActive 
    { 
     get 
     { 
      return _IsActive; 
     } 
     set 
     { 
      _IsActive = value; 
     } 
    } 

    public int? SupplierID 
    { 
     get 
     { 
      return _SupplierID; 
     } 
     set 
     { 
      _SupplierID = value; 
      if (_SupplierID != null) // The county is active if there is a supplier ID matching 
      { 
       _IsActive = true; 
      } 
     } 

    } 
} 

注:類別和category_sub對象都是通過實體框架建立。

我的鏈接表達有2個表,看起來像這樣:

public IEnumerable<SupplierCategories> GetAllOrdered() 
    { 
     IEnumerable<SupplierCategories> areaList; 

     var results = from cat in dbContext.categories 
         join sub in dbContext.category_sub on cat.id equals sub.categoryid 
         orderby cat.id ascending, sub.name ascending 
         select new SupplierCategories 
         { 
          _category = { new category { cat.id, cat.name } }, 
          _category_sub = { new category_sub { sub.id, sub.categoryid, sub.name } } 
         }; 

     areaList = results.ToList(); 

     return areaList; 
    } 

希望你可以看到,我想直接創建「_category」和「_category_sub」對象。

我得到的錯誤是:

錯誤5無法與集合初始化,因爲 它沒有實現 'System.Collections.IEnumerable' d初始化 型 'Essential_Collections.Models.category' :\網站\ Essential Collections \ development \ build \ Essential Collections \ Models \ SupplierCategories \ SupplierCategoriesRepository.cs 51 37基本收藏品

任何解決方案或其他可能的方法將受到歡迎

回答

1

嘗試從而去除多餘的括號:

public IEnumerable<SupplierCategories> GetAllOrdered() 
{ 
    IEnumerable<SupplierCategories> areaList; 

    var results = from cat in dbContext.categories 
        join sub in dbContext.category_sub on cat.id equals sub.categoryid 
        orderby cat.id ascending, sub.name ascending 
        select new SupplierCategories() 
        { 
         _category = cat, 
         _category_sub = sub 
        }; 

    areaList = results.ToList(); 

    return areaList; 
} 
+0

非常感謝,做工精細。但是,如果我想創建不直接映射到表的對象,我可以像上面那樣指定個別值嗎? – Billyhomebase 2010-12-14 16:28:40

+0

@Billyhomebase:@BFree突出了你的類別和category_sub的對象構造函數出錯的地方。假如你有一個合適的構造函數,或者你恰當地識別了你正在初始化的屬性,那麼你可以實例化這些新的對象。 – Lazarus 2010-12-15 12:16:35

+0

如果這個「工作正常」,我建議接受它作爲答案。 – 2011-02-02 18:38:15

1

你錯過了構造函數調用的括號:

select new SupplierCategories() 
{ 
0
public IEnumerable<SupplierCategories> GetAllOrdered() 
{ 
    IEnumerable<SupplierCategories> areaList; 

    var results = from cat in dbContext.categories 
        join sub in dbContext.category_sub on cat.id equals sub.categoryid 
        orderby cat.id ascending, sub.name ascending 
        select new SupplierCategories 
        { 
         _category = new category { cat.id, cat.name } , //removed the curly braces here 
         _category_sub = new category_sub { sub.id, sub.categoryid, sub.name } //removed the curly braces here 
        }; 

    areaList = results.ToList(); 

    return areaList; 
} 

你得到的錯誤是因爲這些花括號我刪除的。這仍然不能編譯,因爲你需要明確地闡明你的對象初始化設置其屬性:

_category = new category { ID = cat.id, Name = cat.name } 

,或者如果您的類別類有一個構造函數重載需要那些指定參數時,再通過這些英寸