【问题标题】:Query is not showing proper result查询未显示正确结果
【发布时间】:2013-05-27 08:28:29
【问题描述】:

查询连接 ProductID 上的两个表。表名为“product”和“SpecialOfferProduct”。我想要的是加入 ID 相同的 ProductID 上的表,从产品中选择名称。我这样做了,但它没有向我显示产品的名称。

 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;

            }

【问题讨论】:

  • 您确定列表 specialOffer 为空吗? (只是为了检查查询或数据网格中的错误)

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


【解决方案1】:

试试这个

  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();

【讨论】:

  • 在上下文中加入 p。c.ProductID 上的产品等于 p.ProductID 这解决了我的问题“无论如何谢谢”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多