如何在LINQ中对两个参数执行连接

时间:2014-03-31 17:38:02

标签: c# linq join

我有以下代码:

var allCountryRates = (from c in allCountryCombinations
                        join r in Db.PaymentRates_VisaImmigrationPermit 
                            on new { c.HomeCountryId, c.HostCountryId } 
                            equals new { r.HomeCountryId, r.HostCountryId }
                        select r);

基本上,如果在r中找到c,则根据联接的两个条件,我想选择r。如果找不到r的{​​{1}},那么我想生成一个c的空记录并选择该记录。

这可能吗?我觉得我非常接近,但不知道如何进一步接受它。

我上面的代码在Guid.NewGuid()上给出了一个错误,它读取了“,join子句中某个表达式的类型不正确。在'Join'调用中类型推断失败。”

参考。 How to do joins in LINQ on multiple fields in single join

编辑最新版本。

join

2 个答案:

答案 0 :(得分:2)

由于您不想要内部联接,而是需要左联接,因此您希望使用GroupJoin运算符而不是Join运算符。

var allCountryRates = (from c in allCountryCombinations
                        join r in Db.PaymentRates_VisaImmigrationPermit 
                            on new { c.HomeCountryId, c.HostCountryId } 
                            equals new { r.HomeCountryId, r.HostCountryId }
                        into matches
                        let match = matches.Any() ? matches.First() : emptyMatch
                        select match);

答案 1 :(得分:1)

您可以尝试这样的事情:  var allCountryRates =(来自于Db.PaymentRates_VisaImmigrationPermit中的r,其中allCountryCombinations.Any(c => c.HomeCountryId == r.HomeCountryId&& c.HostCountryId == r.HostCountryId)选择r).FirstOrDefault();

我创建了一个ConsoleApp来测试你的问题并且它有效。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StackOverFlowConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            List<PaymentRates_VisaImmigrationPermit> PaymentRates_VisaImmigrationPermits = new List<PaymentRates_VisaImmigrationPermit>() { 
            new PaymentRates_VisaImmigrationPermit(){HomeCountryId= new Guid("5fb7097c-335c-4d07-b4fd-000004e2d28a"),HostCountryId=new Guid("5fb7097c-335c-4d07-b4fd-000004e2d28a")},
            new PaymentRates_VisaImmigrationPermit(){HomeCountryId=new Guid("5fb7097c-335c-4d07-b4fd-000004e2d28b"),HostCountryId=new Guid("5fb7097c-335c-4d07-b4fd-000004e2d28b")}
            };
            List<allCountryCombination> allCountryCombinations = new List<allCountryCombination>() {             
            new allCountryCombination(){HomeCountryId=new Guid("5fb7097c-335c-4d07-b4fd-000004e2d28b"),HostCountryId=new Guid("5fb7097c-335c-4d07-b4fd-000004e2d28b")},
            new allCountryCombination(){HomeCountryId=new Guid("5fb7097c-335c-4d07-b4fd-000004e2d28c"),HostCountryId=new Guid("5fb7097c-335c-4d07-b4fd-000004e2d28c")}
            };

            var allCountryRates = (from r in PaymentRates_VisaImmigrationPermits where allCountryCombinations.Any(c => c.HomeCountryId == r.HomeCountryId && c.HostCountryId == r.HostCountryId) select r).FirstOrDefault();

            int sa = 0;
        }

        class PaymentRates_VisaImmigrationPermit
        {
            public Guid? HomeCountryId { get; set; }

            public Guid? HostCountryId { get; set; }
        }

        class allCountryCombination
        {
            public Guid HomeCountryId { get; set; }

            public Guid HostCountryId { get; set; }
        }

    }
}