我有下面的SQL查询,我试图转换为Linq但不能让它完全工作。
select l.nid,
l.sName,
l.language,
coalesce(p.kLanguage, 0) kLanguage
from vLanguage l
left join
(
select pl.kLanguage,
p.nid,
p.sName
from vProductLanguage pl
left join vProduct p
on pl.kProduct = p.nid
where p.nid = 1
) p
on l.nid = p.kLanguage
where l.bClosed =0
我已经在我的WCF服务
中管理了这个 [WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "ProductLanguageList")]
public List<LookUpProductLanguage> GetProductLanguageList()
{
var passedProductId = int.Parse("12");
var query = from languageEntity in _languageEntityRepository.AsQueryable()
join subQueryResult in (from productLanguageEntity in _productLanguageEntityRepository.AsQueryable() join productEntity in _productRepository.AsQueryable() on productLanguageEntity.LanguageProductId equals productEntity.Id into joinedProductLanguage
from productLanguageJoin in joinedProductLanguage.DefaultIfEmpty() where productLanguageJoin.Id.Equals(passedProductId)
select new {LanguageId = productLanguageEntity.LanguageId}
) on languageEntity.Id equals subQueryResult.LanguageId
return null;
}
目前我已经返回null但是想要返回sql查询中提到的列。我在行连接subQueryResult附近得到错误“无法从查询中推断出类型参数”。我在这是要干嘛?请纠正我,因为我确信我做错了。
答案 0 :(得分:1)
最后管理。这是我的查询。
[WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "ProductLanguageList")]
public List<LookUpProductLanguage> GetProductLanguageList(GetParameters param)
{
var passedProductId = int.Parse(param.ProductId);
var query = from languageEntity in _languageEntityRepository.AsQueryable()
join subQueryResult in
(from productLanguageEntity in _productLanguageEntityRepository.AsQueryable()
join productEntity in _productRepository.AsQueryable() on
productLanguageEntity.LanguageProductId equals productEntity.Id into
joinedProductLanguage
from productLanguageJoin in joinedProductLanguage.DefaultIfEmpty()
where productLanguageJoin.Id.Equals(passedProductId)
select new {productLanguageEntity.LanguageId}
) on languageEntity.Id equals subQueryResult.LanguageId into a
from b in a.DefaultIfEmpty()
where languageEntity.Closed == 0
select new LookUpProductLanguage {LanguageId = languageEntity.Id, LanguageName = languageEntity.Name,LanguageCode = languageEntity.Language,LanguageProductId = b.LanguageId };
return query.ToList();
}
非常感谢那些努力回答我的人。