简体   繁体   English

LinqToSQL左外部联接

[英]LinqToSQL Left outer join

I want to replicate this sql query but i'm having dificulty trying to find the solution; 我想复制这个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

This is where I've got up to but the error I get is "Object reference no set to in instance of an object". 这是我要去的地方,但是出现的错误是“对象引用未设置为对象实例”。

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();

Sorry I'm very much a newbie. 对不起,我非常新手。 ClientProperties is defined as this as its used to collate data from a collation of csv files. ClientProperty的定义是,它用于从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;

    }

Something like this?: 像这样吗?

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();

I supose your "ClientProperties()" object is a context or something like that. 我认为您的“ ClientProperties()”对象是上下文或类似的东西。 In this case you need do something like: 在这种情况下,您需要执行以下操作:

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();

}

And then you can access to the object easily: 然后,您可以轻松访问该对象:

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

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

Hi Thanks for all you advice as it helped me worked it out this was my end result; 嗨,谢谢您的建议,因为它可以帮助我解决这个问题,这是我的最终结果。

            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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM