【问题标题】:Writing left join Query in LINQ在 LINQ 中编写左连接查询
【发布时间】:2013-05-05 05:24:01
【问题描述】:
select a.stakebuyinid , a.StakeBuyInValue  from StakeBuyInByStakeCategories  AS b 
left join StakeBuyIns AS a on b.stakebuyinid = a.stakebuyinid 
where b.GametypeId = 1 and b.StakeCategoryID = 3 and a.Currencyid = 1

以上是我想用 LINQ 编写的简单 SQL 查询

我正在使用以下 LINQ 查询,但引发错误:-“转换为值类型 'Int32' 失败,因为具体化值为空。结果类型的泛型参数或查询必须使用可为空的类型。”

var query = (from v in db.StakeBuyInByStakeCategories.Where(x => x.GameTypeId == gametypeid && x.StakeCategoryId == stakecategoryid)
                        join u in db.StakeBuyIns.Where(y => y.CurrencyId == currencyid)
                               on v.StakeBuyInId equals u.StakeBuyInId into Temp
                           from vus in Temp.DefaultIfEmpty()
                       select new {
                           vus.StakeBuyInId,
                           vus.StakeBuyInValue )

【问题讨论】:

  • 好的。您尝试了什么,问题出在哪里?

标签: c# asp.net-mvc linq


【解决方案1】:

假设
StakeBuyInByStakeCategoriesList as IEnumerable<StakeBuyInByStakeCategories>StakeBuyInsList as IEnumerable<StakeBuyIns>

(from b in StakeBuyInByStakeCategoriesList 
 from a in StakeBuyInsList    
.Where(b.stakebuyinid equals a.stakebuyinid)
.DefaultIfEmpty()    
.Where( b.GametypeId == 1 and b.StakeCategoryID == 3 and a.Currencyid == 1)    
select new {Stakebuyinid=a.stakebuyinid, StakeBuyInValue=a.StakeBuyInValue}

【讨论】:

    【解决方案2】:

    模型中的 int 应该是 int?,因为可以为 null 的返回值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      相关资源
      最近更新 更多