Linq to SQL选择新模式

时间:2014-06-22 19:33:38

标签: c# sql linq

是否可以执行一个查询以使用该模式检索多个数据库值?

我有一个视图模型调用: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和功能集合。你能救我吗?

1 个答案:

答案 0 :(得分:0)

鉴于您尝试在viewmodel中选择每个集合的整体,您不需要linq表达式。你可以直接新建一个viewmodel,即:

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