C# 如何通过检查id是否存在来获取SQL Server或C中的新记录

C# 如何通过检查id是否存在来获取SQL Server或C中的新记录,c#,sql-server,linq,C#,Sql Server,Linq,如何通过检查OrgId是否在TransactionDate之前来获取数据库中的记录 这里是我当前的SQL代码。但在SQL Server中找到解决方案后,我想将其应用到C LINQ查询中 select refno, OrgId, typeCode, fullnames from Transactions where TransactionDate >= '01/06/2015' and OrgId not in (select OrgId from

如何通过检查OrgId是否在TransactionDate之前来获取数据库中的记录

这里是我当前的SQL代码。但在SQL Server中找到解决方案后,我想将其应用到C LINQ查询中

select 
    refno, OrgId, typeCode, fullnames 
from 
    Transactions 
where 
    TransactionDate >= '01/06/2015'
    and OrgId not in (select OrgId from Transactions 
                      where TransactionDate < '01/06/2015')

我不确定您的查询以及它的作用,但我可以为您提供linq。根据您的数据结构,TransactionDate>=date可能已经暗示了第二个条件

var date = new DateTime("01/06/2015");
var result = Transactions.Where(x => x.TransactionDate >= date && 
    !Transactions.Where(y => y.TransactionDate < date)
            .Select(y => y.OrgId).Contains(x.OrgId))
        .Select(x => new {
            refno = x.refno,
            OrgId = x.OrgId,
            typeCode = x.typeCode,
            fullnames = x.fullnames
        });
这也是一种匿名类型,我可能会考虑创建一个类,以便将结果选择到其中

public class Result{
    public int refno { get; set; }
    public int OrgId { get; set; }
    public string typeCode { get; set; }
    public string fullnames { get; set; }
}

var date = new DateTime("01/06/2015");
var result = Transactions.Where(x => x.TransactionDate >= date && 
    !Transactions.Where(y => y.TransactionDate < date)
            .Select(y => y.OrgId).Contains(x.OrgId))
        .Select(x => new Result(){
            refno = x.refno,
            OrgId = x.OrgId,
            typeCode = x.typeCode,
            fullnames = x.fullnames
        });

你的问题没有道理。OrgId大概是一个数字。怎么可能小于TransactionDate,而TransactionDate可能是一个日期。@GordonLinoff对不起。我的意思是交易日期比正式日期短。正如你在代码中看到的。你能帮帮我吗?谢谢你它很管用。你帮了我,非常感谢。上帝保佑。在过去的三周里,我一直在努力解决这个问题,我在网上寻找帮助,但没有人给我正确的答案。但昨天我决定提出一个sql查询,至少有人能理解我想要什么。你给了我我想要的谢谢。