我如何将 "or" 运算符引入 linq 查询连接

How and I introduce an "or" operator into a linq query join

将 "or" 运算符引入 linq 查询连接的语法是什么。好像不喜欢“||”,"or"?我需要将一个外键与来自用户 table.

的两个可能的键相结合
var thirdQuery = (from u in tbl_users
join ua in tbl_userDocuments on (u.TransportUserID equals ua.fkDocumentID
                             || u.WorkUserID equals ua.fkDocumentID) into uaGroup
from uaTrans in uaGroup.DefaultIfEmpty()
join ul in tbl_user_logins on uaTrans.fkUserID equals ul.user_login_id_pk into ulGroup

没有 or 部分,这有效但缺少额外的键

var thirdQuery = (from u in tbl_users
join ua in tbl_userDocuments on (u.TransportUserID equals ua.fkDocumentID) into uaGroup
from uaTrans in uaGroup.DefaultIfEmpty()
join ul in tbl_user_logins on uaTrans.fkUserID equals ul.user_login_id_pk into ulGroup

因此您的数据库至少包含两个 table:UsersUserDocuments。每个 UserDocument 都有一个 属性 fkDocumentId

虽然你没这么说,但在我看来,这是 table Users 中元素的外键。显然这个外键有时指的是 User.TransportUserId 有时指的是 User.WorkUserId.

你确定要这个吗?如果 fkDocumentId 的值为 10,它是指 TransportId 等于 10 的用户,还是指 WorkUserId 等于 10 的用户,还是两者?

无论如何,如果您调查 Enumerable.Join,,您会发现您提供了两个 keySelector:一个用于 select 来自 Users 的密钥,一个用于 select 来自 UserDocuments 的密钥。当这两个键相等时,ResultSelector 参数用于构造您的连接元素。

问题出在字"are equal"上。您必须确保 select 您的密钥并提供 IEqualityComparer 以便密钥将被视为相等。

另一个更简单的方法是创建一个新的 Join 扩展方法。

IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult> Join(
    this IEnumerable<TSource> outer,
    IEnumerable<TInner> inner,
    Func<TOuter, TKey> outerKeySelector1,
    Func<TOuter, TKey> outerKeySelector2,
    Func<TInner, TKey> innerKeySelector,
    Func<TOuter, TInner, TResult> resultSelector)
{
    // make two lookup tables from Outer: one for key1 and one for key2:
    var lookup1 = outer.ToLookup( outerElement => outerKeySelector1(outerElement));
    var lookup2 = outer.ToLookup( outerElement => outerKeySelector2(outerElement));

    // so if you have a TKey from the innerKeySelector, you can find if it matches key1 or key2
    foreach (TInner innerElement in inner)
    {
        TKey innerKey = innerKeySelector(innerElement)
        var matchingElementsKey1 = lookup1[innerKey];
        var matchineElementsKey2 = lookup2[innerKey];
        var allmatchingElements = matchingElementsKey1.Concat(matchingElementsKey2);

        foreach(var matchingElement in allMatchingElements)
        {
            TResult result = resultSelector(matchingElement, innerElement);
            yield return result;
        }
    }
}

用法:

var result = tblUsers.Join(tblUserDocuments,
    user => user.TransportUserId,           // select outerKey1
    user => user.WorkUserId,                // select outerKey2
    document => document.fkDocumentId,      // select innerKey

    // when inner key matches either outerKey1, or outerKey2, create one new object:
    (user, document) => new
    {
        // Select the user documents that you want:
        UserId = user.Id,
        Name = user.Name,
        ...

        // Select the document properties that you want:
        DocumentId = document.Id,
        Author = document.Author,
        PublishedDate = document.Date,
        ...
    })