【问题标题】:LinqToSQL Left outer joinLinqToSQL 左外连接
【发布时间】:2019-06-25 15:59:29
【问题描述】:

我想复制这个 sql 查询,但我很难找到解决方案;

SELECT C.Propref
FROM [dbo].[ClientProperties] C
LEFT OUTER JOIN  [dbo].[Properties] P ON C.[PROPREF] = P.[PROPREF] AND P.Contract = 'TXT'
WHERE P.[PROPREF] IS null

这就是我要解决的问题,但我得到的错误是“对象引用未设置为对象实例”。

var query = (from c in ClientProperties()
                    join p in db.Properties.Where(wc => wc.Contract == _contractId) on c.Place_reference equals p.Theirref into cp
                    from found in cp.DefaultIfEmpty()
                    select new
                    {
                        UPRN = c.Place_reference,
                    }).ToList();

对不起,我是个新手。 ClientProperties 被定义为它用于从 csv 文件的整理中整理数据。

    private IEnumerable<ClientProperty> ClientProperties()
    {
        CsvContext cc = new CsvContext();

        if (Directory.Exists(_interfaceInProperty))
        {
            IEnumerable<ClientProperty> properties = new List<ClientProperty>();
            var files = Directory.GetFiles(_interfaceInProperty, "Prop*.csv");

            foreach (var f in files)
            {
                properties = cc.Read<ClientProperty>(f, inputFileDescription);
            }
            return properties;
        }

        return null;

    }

【问题讨论】:

标签: c# linq-to-sql


【解决方案1】:

像这样?:

var query = db.ClientProperties.GroupJoin(
    db.Properties,
    a => a.PROPREF,
    b => b.PROPREF,
    (a, b) => new { ClientProperties = a, Properties = b })
    .SelectMany(x => x.ClientProperties.Where(y => y.Contract == "TXT" && string.IsNullOrEmpty(y.PROPREF.ToString())),
    (a, b) => new { ClientProperties = a, Properties = b }).ToList();

我假设您的“ClientProperties()”对象是一个上下文或类似的东西。在这种情况下,您需要执行以下操作:

using (var db = new ClientProperties())
{

    var query = db.ClientProperties.GroupJoin(
        db.Properties,
        a => a.PROPREF,
        b => b.PROPREF,
        (a, b) => new { ClientProperties = a, Properties = b })
        .SelectMany(x => x.ClientProperties.Where(y => y.Contract == "TXT" && string.IsNullOrEmpty(y.PROPREF.ToString())),
        (a, b) => new { ClientProperties = a, Properties = b }).ToList();

}

然后您就可以轻松访问该对象了:

var response = query.FirstOrDefault().ClientProperties.Propref;

foreach (var item in query)
{
    var each_response = item.ClientProperties.Propref;
}

【讨论】:

  • 对不起,我是个新手,我已经添加了更多关于 ClientProperties 的详细信息。
【解决方案2】:

您好,感谢您的所有建议,因为它帮助我解决了问题,这是我的最终结果;

            var query = (from c in ClientProperties()
                join p in db.Properties.Where(wc => wc.Contract == _contractId) on c.PROPREF equals p.PROPREF into cp
                from found in cp.DefaultIfEmpty()
                where found == null
                select new
                {
                    UPRN = c.PROPREF,
                    Address = c.Location_address_1
                }).ToList();

【讨论】:

    猜你喜欢
    • 2010-11-11
    • 2011-10-01
    • 2016-10-02
    • 1970-01-01
    • 2011-09-10
    • 2014-02-24
    • 2019-01-22
    • 2016-10-29
    • 2011-08-10
    相关资源
    最近更新 更多