使用List<>加入LINQ在选择新的

时间:2011-05-01 20:04:53

标签: c# linq linq-to-sql

嗨,我第一次问这里,所以我试着正确地做到这一点

我有一个问题,我正在制作一个购物篮,我几乎在那里,但总是一个但

我想拥有的是这样的东西

List<HKurv> KurvInnhold = (List<HKurv>)Session["KurvInnhold"];
DataClasses1DataContext db = new DataClasses1DataContext();

if (Session["KurvInnhold"] != null)
        {
            var query = from a in db.Cabinets
                        from b in db.Commodities
                        from e in db.sArticleNumbers
                        from d in KurvInnhold

                        where
                        d.VareKjøpt.Contains(e.ArtNum) &&
                        a.ArticleNumberID == e.ID &&
                        a.ArticleNumberID == b.ArticleNumberID

                        select new
                        {
                            BestiltAntall = d.AntallValgt,
                            Price = b.Price,
                            ModelName = a.ModelName,
                        };

            Handlekurv1.DataSource = query;
            Handlekurv1.DataBind();
        }

但它不允许使用db和list&lt;&gt;在同一个查询中

解决!修改了马格努斯的答案

            var kjopKollonne = from p in KurvInnhold
                               select p.VareKjøpt;

            var query1 = (from a in db.Cabinets
                         from b in db.Commodities
                         from e in db.sArticleNumbers

                         where
                         kjopKollonne.Contains(e.ArtNum) &&
                         a.ArticleNumberID == e.ID &&
                         a.ArticleNumberID == b.ArticleNumberID
                        select new
                         {
                              ArtNum = e.ArtNum,
                              Price = b.Price,
                              ModelName = a.ModelName,
                         }).ToList();

            var query2 = from a in query1
                         join b in KurvInnhold on a.ArtNum equals b.VareKjøpt
                         select new
                         {
                              BestiltAntall = b.AntallValgt,
                              Price = a.Price,
                              ModelName = a.ModelName,
                         };

            Handlekurv1.DataSource = query2;
            Handlekurv1.DataBind();

1 个答案:

答案 0 :(得分:1)

试试这个:

var query1 = from a in db.Cabinets
  from b in db.Commodities
  from e in db.sArticleNumbers
  from d in KurvInnhold
  where
  KurvInnhold.Select(k => k.VareKjøpt).Contains(e.ArtNum) &&
  a.ArticleNumberID == e.ID &&
  a.ArticleNumberID == b.ArticleNumberID

 select new
  {
       ArtNum = e.ArtNum,
       Price = b.Price,
       ModelName = a.ModelName,
  }.ToList();

var query2 = 
  from a in query1
  join b in KurvInnhold on a.ArtNum equals b.VareKjøpt
  select new
  {
       BestiltAntall = b.AntallValgt,
       Price = a.Price,
       ModelName = a.ModelName,
  };

Handlekurv1.DataSource = query2;
Handlekurv1.DataBind();