2014-06-22 89 views
0

是否可以執行一個查詢以檢索具有該模式的多個數據庫值?Linq to SQL選擇新模式

我有一個視圖模型調用:FreemimumViewModel。內部視圖模型我有:

public class FreemiumViewModel 
{ 
    public FreemiumViewModel() 
    { 
     Skus = new HashSet<StockKeepingUnit>(); 
     Features = new HashSet<Feature>(); 
     RowValues = new Dictionary<string, object>(); 
     ColumnTitles = new List<string>(); 
     CanUsePromoCode = true; 
     SelectionMatrix = new HashSet<FeatureStockKeepingUnitViewModel>(); 
    } 

    public ICollection<Feature> Features { get; set; } 
    public ICollection<StockKeepingUnit> Skus { get; set; } 
    public ICollection<FeatureStockKeepingUnitViewModel> SelectionMatrix { get; set; } 
    public UserProfile UserProfile { get; set; } 
    public UserSubscriptionViewModel UserSubscriptionViewModel { get; set; } 
    public bool CanUsePromoCode { get; set; } 
    public string PromotionalCode { get; set; } 
    /// <summary> 
    /// StockKeepingUnit name 
    /// </summary> 
    public List<string> ColumnTitles { get; set; } 
    /// <summary> 
    /// Key is the feature row title and object is the stock column value. 
    /// </summary> 
    public Dictionary<string, object> RowValues { get; set; } 
} 

現在我想收集與語法的所有數據庫的信息:

var vModel = (from skus in Context.Instance.StockKeepingUnits 
         from features in Context.Instance.Features 
         ... 
         select new FreemiumViewModel() 
         { 
          Skus = skus, // Need to be ICollection 
          Features = features // Need to be ICollection 
          ... 
         }).SingleOrDefault(); 

我嘗試做一個請求到數據庫,而不是多,但我沒有我的skus和功能的集合。你可以幫我嗎?

+0

對於您的視圖模型集合,它看起來像你正試圖加載每個從數據庫中收藏的全部,不應用任何過濾器。這是你想要做的嗎? – devduder

+0

是的,這是我需要的。 – Karine

回答

0

鑑於您試圖將每個集合的全部選擇到您的視圖模型中,您不需要linq表達式。你可以只新一個視圖模型直接,即:

var viewmodel = new FreemiumViewModel 
    { 
     Skus = Context.Instance.StockKeepingUnits.ToList(), 
     Features = Context.Instance.Features.ToList() 
     ... 
    };