【问题标题】:How do that (simple?) linq query怎么做(简单?)linq查询
【发布时间】:2012-04-19 13:35:39
【问题描述】:

我要简化数据库。
这是它的样子:

表格:

Main: Products: ID, Ref, Price, 
Translation: ID, Product_ID, Language_ID, Name
Images: ID, Product_ID, Path, Index

我是 linq 的新手,我尝试用它来检索所有产品,它们的名称为 language_ID = 1 以及索引 = 1 的图像

From p In db.Products 
Join t In db.Translate_Products On p.ID_Product Equals t.Product_ID 
Join i In db.Images On p.ID_Product Equals i.Product_ID 
Where t.Language_ID = 1 And i.Index= 0 
Select p, t, i

【问题讨论】:

  • 你应该问“我如何接受答案”10 分中有 0 分令人印象深刻!
  • 13 个问题,0 个接受,您可能需要考虑阅读 StackExchange 的工作原理
  • 好的!我承认我从来没有注意过!现在我知道这很重要!

标签: .net sql vb.net linq


【解决方案1】:
From p In db.Products 
Join t In db.Translate_Products On p.ID_Product equals t.Product_ID into results1
from r1 in results.DefaultIfEmpty()
Join i In db.Images On p.ID_Product equals i.Product_ID 
into results2
from r2 in results2.DefaultIfEmpty()
Where results.Language_ID = 1 And i.Index= 0 
Select new
{

Productid = p.Productid,
..
..
..
}

【讨论】:

  • 实际上我无法让它工作......Visual Studio 不允许我在第二行有一个“进入”......注意一定要理解它是如何工作的......跨度>
  • into 在 LINQ 中有效。验证 p.ID_Product 和 t.Product_ID 是否属于同一类型。并分享您收到的错误消息。
  • Visual Studio 说“指令结束”(在 bleu 下划线)
  • 这会是一些语法错误..你需要检查它..但肯定不是关键字问题...
【解决方案2】:
From p In db.Products 
Join t In db.Translate_Products On p.ID_Product equals t.Product_ID 
into results1
from r1 in results1.DefaultIfEmpty()
Join i In db.Images On p.ID_Product equals i.Product_ID     
Where t.Language_ID == 1 And i.Index== 0 
Select new
{

Productid = p.Productid,
..
..
..
}

【讨论】:

  • Thanx 但仍然无法正常工作...只是从 p 在 db.Products 中加入 t 在 db.Translate_Products 上 p.ID_Product 等于 t.Product_ID 到 results1 给我一个错误的结果...是vb吗?
  • 它是 C# 代码..我使用相同的查询,但不同的表,它对我来说非常好..不知道你的案例伙伴..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
  • 1970-01-01
相关资源
最近更新 更多