linq方法语法多个左键[英] LINQ method syntax for multiple left join

本文是小编为大家收集整理的关于linq方法语法多个左键的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

需要将三个表连接在一起.

Table [Package]

ID (int)
ContainerID (int)
Code (string)
Code2 (string)


Table [UserHasPackages]

UserID (Comes from Identity tables) (string)
PackageID (int)


Table [Container]

ID (int)
Name (string)
Description (string)

进入代表我想在我的视图中显示的对象的ViewModel:

public class CustomViewModel
{
    public int ID { get; set; }
    public string Name { get; set; } // Container.Name
    public string Code { get; set; } // Package.Code
    public string Code2 { get; set; } // Package.Code2
}

但是我在加入时遇到问题:

List<CustomViewModel> list = new List<CustomViewModel>();

        list = context.Packages.Join(
            context.Containers,
            p => p.ContainerID,
            c => c.ID,
            (p, c) => new { p, c})
        .Join(                                 //error is here
            context.UserHasPackages,
            a => a.p.ID,
            b => b.ApplicationUserId,
            (a, b) => new { a, b })
        .Select(f => new CustomViewModel
        {
            ID = f.p.ID,
            Name = f.c.Name,
            Code = f.p.Code,
            Code2 = f.p.Code2
        }).ToList();

Type arguments for method cannot be inherited from the usage. Try specifying the type arguments explicitly.

是否有另一种方法可以进行两次连接(如上所述),这是一种更加行为的方法?


解决方案

基于方法的语法在这里是不做的,必须使用查询语法:

var query = (from package in context.Packages
        join container in context.Containers on package.ContainerID equals container.ID
        join userHasPackage in context.UserHasPackages on package.ID equals userHasPackage.PackageID
        where userHasPackage.UserID == "SomeUser"
        select new CustomViewModel
        {
            ID = package.ID,
            Name = container.Name,
            Code = package.Code,
            Code2 = package.Code2
        }).ToList();

推荐答案

我假设您要加入UserHaspackages表,因为您想过滤特定用户的结果(我只是放入'someuser',因为我不确定'userhaspackages.papplicationuserid' "来自),因为它不包含在视图模型中.

我相信以下内容应该有效:

var list = context.Packages
    .Join(context.Containers, p => p.ContainerID, c => c.ID, (p, c) => new { p, c })
    .Join(context.UserHasPackages, pc => pc.p.ID, u => u.PackageID, (pc, u) => new { pc.p, pc.c, u })
    .Where(pcu => pcu.u.UserID == "SomeUser")
    .Select(pcu => new
    {
        pcu.p.ID,
        pcu.c.Name,
        pcu.p.Code,
        pcu.p.Code2
    });

您也可以使用查询语法来执行此操作:

var query = from package in context.Packages
            join container in context.Containers on package.ContainerID equals container.ID
            join userHasPackage in context.UserHasPackages on package.ID equals userHasPackage.PackageID
            where userHasPackage.UserID == "SomeUser"
            select new
            {
                package.ID,
                container.Name,
                package.Code,
                package.Code2
            };

本文地址:https://www.itbaoku.cn/post/1556874.html

问题描述

Three tables are needed to be joined together.

Table [Package]

ID (int)
ContainerID (int)
Code (string)
Code2 (string)


Table [UserHasPackages]

UserID (Comes from Identity tables) (string)
PackageID (int)


Table [Container]

ID (int)
Name (string)
Description (string)

Into a viewmodel that represents an object I'd like to display in my view:

public class CustomViewModel
{
    public int ID { get; set; }
    public string Name { get; set; } // Container.Name
    public string Code { get; set; } // Package.Code
    public string Code2 { get; set; } // Package.Code2
}

But I'm having problems doing the join:

List<CustomViewModel> list = new List<CustomViewModel>();

        list = context.Packages.Join(
            context.Containers,
            p => p.ContainerID,
            c => c.ID,
            (p, c) => new { p, c})
        .Join(                                 //error is here
            context.UserHasPackages,
            a => a.p.ID,
            b => b.ApplicationUserId,
            (a, b) => new { a, b })
        .Select(f => new CustomViewModel
        {
            ID = f.p.ID,
            Name = f.c.Name,
            Code = f.p.Code,
            Code2 = f.p.Code2
        }).ToList();

Type arguments for method cannot be inherited from the usage. Try specifying the type arguments explicitly.

Is there another way to do two joins (as described) that's the more-proper way?


SOLUTION

Method based syntax is a no-go here, had to go with query syntax:

var query = (from package in context.Packages
        join container in context.Containers on package.ContainerID equals container.ID
        join userHasPackage in context.UserHasPackages on package.ID equals userHasPackage.PackageID
        where userHasPackage.UserID == "SomeUser"
        select new CustomViewModel
        {
            ID = package.ID,
            Name = container.Name,
            Code = package.Code,
            Code2 = package.Code2
        }).ToList();

推荐答案

I'm assuming that you want to join the UserHasPackages table because you wanted to filter the results for a specific user (I just put in a 'SomeUser' because I'm not sure where the 'UserHasPackages.ApplicationUserId' came from) since it is not included on the view model.

I believe something like the following should work:

var list = context.Packages
    .Join(context.Containers, p => p.ContainerID, c => c.ID, (p, c) => new { p, c })
    .Join(context.UserHasPackages, pc => pc.p.ID, u => u.PackageID, (pc, u) => new { pc.p, pc.c, u })
    .Where(pcu => pcu.u.UserID == "SomeUser")
    .Select(pcu => new
    {
        pcu.p.ID,
        pcu.c.Name,
        pcu.p.Code,
        pcu.p.Code2
    });

You could also do this using the query syntax:

var query = from package in context.Packages
            join container in context.Containers on package.ContainerID equals container.ID
            join userHasPackage in context.UserHasPackages on package.ID equals userHasPackage.PackageID
            where userHasPackage.UserID == "SomeUser"
            select new
            {
                package.ID,
                container.Name,
                package.Code,
                package.Code2
            };