让我说我有这样的名字执行表:
InvoiceID------ExecutionID-------IsSettled
123-----1-----0
123-----2-----1
345-----3-----1
345-----4-----1
567-----5-----0
567-----6-----0
我的问题:
什么是仅检索InvoiceID的查询,其中所有执行的IsSettled = 1。 我的意思是查询的结果应该是这样的:
345-----3-----1
345-----4-----1
我想要执行任何具有isSettled flog = 0执行的发票,在我的问题中,你会发现invocieID = 123有2次执行,一次是IsSettled flag = 0,另一次执行带有Issettled标志= 1,所以我不希望在我的结果集中包含此发票,因为它有一个isSettled flag = 0
的执行如果有人知道我是否有执行对象,我如何使用Linq获得相同的结果。
查询可以是SQL或LINQ
由于
答案 0 :(得分:1)
列出未结算的发票ID:
var notNeeded = ExecutionObject.Where(e => e.IsSettled == 0).Select(s => s.InvoiceId).ToList();
然后过滤已结算的发票,并确保发票ID在未结算的清单中。
var invoices = ExecutionObject.Where(e => e.IsSettled == 1 && !notNeeded.Contains(e.InvoiceId)).ToList();
答案 1 :(得分:1)
查询可以是SQL或LINQ
<强> Query
强>
select * from Executions
where InvoiceID in
(
select InvoiceID from Executions
group by InvoiceID
having min(Issettled)=1
)
<强> SQL FIDDLE 强>
答案 2 :(得分:0)
如果这是一个linq查询(您还没有告诉我们),那么您可以使用: -
var settled = executions.GroupBy(id => id.invoiceid).Where(inv => inv.All(s => s.issettled)).Select(x => x).ToList();
答案 3 :(得分:0)
在理解了问题并在LinqPad中进行操作后,以下Linq查询将获得您所需的信息:
Executions.GroupBy(e => e.InvoiceId)
.Where(g => g.All(e => e.IsSettled == true))
.SelectMany(g => g)
linq脚本可在此处使用:http://share.linqpad.net/fawl6l.linq
答案 4 :(得分:0)
考虑一下:
var invoices = (from execution in ExecutionObject
where execution.IsSettled == 1
&& !ExecutionObject.Where(x=>x.IsSettled == 0).Select(y=>y.InvoiceID).Contains(execution.InvoiceID)
select execution.InvoiceID).Distinct().ToList();
我还没有对它进行测试,但想法是先通过IsSettled == 1进行过滤,然后删除任何有IsSettled == 0的记录。
答案 5 :(得分:-4)
我认为这里的关键点是你想要没有结算= 0的发票吗?
select distinct InvoiceID
from executions
where invoiceID <> ALL(select invoice_id from executions where is_settled = 0)
或在linq
var q = from i in ctx.Invoices
where ctx.Invoices.All(x => is_settled == 1 || x.invoice_id != i.invoice_id)
select i.invoice_id;
var result = q.Distinct();