# 求一条linq语句。

0
[已解决问题] 解决于 2018-01-02 13:33

var expression = from d in this.db.MS_Deposit
join g in this.db.MS_Gold on d.Account equals g.Account
where d.IsDeposit == 1 && d.Status == 1 && g.GoldStatus == 11
&& d.CreateTime >= start && d.CreateTime <= end && g.CreateTime >= start && g.CreateTime <= end
select new { Account = d.Account, Deposit = d.MjMoney, Gold = g.MjMoney, CreateTime = d.CreateTime };

this.db.MS_Deposit 按条件有1条数据。

this.db.MS_Gold 按条件有0条数据。

Eysa | 初学一级 | 园豆：62

0

on .... into tmp

from obj in tmp.DefaultIfEmpty()

where....

Daniel Cai | 专家六级 |园豆：10424 | 2017-12-06 11:19

var expression1 = from d in this.db.MS_Deposit
join g in this.db.MS_Gold on d.Account equals g.Account into gsItems
from gs in gsItems.DefaultIfEmpty()
where d.IsDeposit == 1 && d.Status == 1 && gs.GoldStatus == 11
&& d.CreateTime >= start && d.CreateTime <= end && gs.CreateTime >= start && gs.CreateTime <= end
select new { Account = d.Account, Deposit = d.MjMoney, Gold = gs.MjMoney, CreateTime = d.CreateTime };

Eysa | 园豆：62 (初学一级) | 2017-12-06 11:25

@Eysa: 你把右表的where条件提到前面去

join g in MS_Gold.Where(g=>...)

on.....

Daniel Cai | 园豆：10424 (专家六级) | 2017-12-06 14:46

@Daniel Cai:

var expression = from d in this.db.MS_Deposit.Where(d => d.IsDeposit == 1 && d.Status == 1 && d.CreateTime >= start && d.CreateTime <= end)
join g in this.db.MS_Gold.Where(g => g.GoldStatus == 11 && g.CreateTime >= start && g.CreateTime <= end) on d.Account equals g.Account
into gsItems
from gs in gsItems.DefaultIfEmpty()
select new { Account = d.Account, Deposit = d.MjMoney, Gold = gs.MjMoney, CreateTime = d.CreateTime };

Eysa | 园豆：62 (初学一级) | 2017-12-06 15:35

@Eysa: 不是这样的

from a in tbA join b in tbB.Where(item=>item.condition....)

on a... equals b..... into tmp

where a.condition.....

from c in tmp.DefaultIfEmpty()

select new {pro1=c.pro1,pro2=c.pro2....};

Daniel Cai | 园豆：10424 (专家六级) | 2017-12-06 15:41

@Daniel Cai: 到值类型“System.Decimal”的强制转换失败，因为具体化值为 null。结果类型的泛型参数或查询必须使用可以为 null 的类型。

var expression = from d in this.db.MS_Deposit
join g in this.db.MS_Gold.Where(g => g.GoldStatus == 11 && g.CreateTime >= start && g.CreateTime <= end) on d.Account equals g.Account
into gsItems
where d.IsDeposit == 1 && d.Status == 1 && d.CreateTime >= start && d.CreateTime <= end
from gs in gsItems.DefaultIfEmpty()
select new { Account = d.Account, Deposit = d.MjMoney, Gold = gs.MjMoney, CreateTime = d.CreateTime };

Eysa | 园豆：62 (初学一级) | 2017-12-06 17:47

@Eysa: 最后你是从gs中拿数据，不是从你前面的两个对象中抽数据

Daniel Cai | 园豆：10424 (专家六级) | 2017-12-06 17:57

@Daniel Cai: 这个gs貌似只有MS_Gold的数据啊，那我要MS_Deposit的怎么办？

Eysa | 园豆：62 (初学一级) | 2017-12-07 17:38

@Eysa: 。。。。

d.Account....

Daniel Cai | 园豆：10424 (专家六级) | 2017-12-07 17:51

您需要登录以后才能回答，未注册用户请先注册