查询未显示正确的结果

时间:2013-05-27 08:28:30

标签: linq linq-to-sql linq-to-entities

Query在ProductID上连接两个表。表名是“product”和“SpecialOfferProduct”。我想要的是在ProductID上连接表,其中Id是相同的,从Product选择Name。我这样做了,但没有向我展示产品的名称。

 private void Button_Click_8(object sender, RoutedEventArgs e)
        {
            using (LinqContainer context = new LinqContainer())
            {
                List<Product> pro=new List<Product>() ;


                var specialOffer = (from c in context.SpecialOfferProducts
                                           join p in pro on c.ProductID equals p.ProductID
                                            select new {p.Name})
                                            .ToList();
                grid.ItemsSource = specialOffer;

            }

1 个答案:

答案 0 :(得分:0)

试试这个

  var specialOffer = (from c in context.SpecialOfferProducts
                                       join p in context.Products on c.ProductID equals p.ProductID
                                        select p.Name)
                                        .ToList();

而不是

  var specialOffer = (from c in context.SpecialOfferProducts
                                       join p in pro on c.ProductID equals p.ProductID
                                        select new {p.Name})
                                        .ToList();