使用LAMBDA左联接以在API中获得结果

时间:2019-02-28 08:05:24

标签: asp.net-web-api lambda left-join

如何使用LAMBDA将以下代码中的Join实现到C#中

Select 
        VD.Id
        , VD.BusinessAddress
        , VD.BusinessDesc
        , VD.BusinessEmail
        , VD.BusinessName
        , VD.BusinessZip
        , VD.ContactPerson
        , VD.ContactNo
        , VD.ProfileUrl
        , L.Name
        , BC.BusinessCategory

        from vendorDomain VD WITH(NOLOCK)
        left Join Location L WITH(NOLOCK) ON VD.City = L.Id
        left join Business_Category BC WITH(NOLOCK) ON VD.BusinessCategory = BC.BusinessId

where VD.IsDeleted = 0

我必须在以下API中实现加入操作:

[HttpGet]
    public async Task<IActionResult> Get()
    {
        var VendorList =await _vendorRepository.Query().Where(x => x.IsDeleted == false).ToListAsync();

        return Ok(VendorList);

    }

那里有很多示例,但是对于新手开发人员来说是一种困惑。

编辑:

这是我到目前为止尝试过的:

var employees = from vndr in context.vendorDomain
                        join C in context.Location on vndr.City equals C.Id into dep
                        from dept in dep.DefaultIfEmpty()

                        select new

                        {
                            vndr.BusinessAddress,
                            vndr.BusinessDesc,
                            vndr.BusinessEmail,
                            vndr.BusinessName,
                            vndr.BusinessWebsite,
                            vndr.BusinessZip,
                            vndr.ContactNo,
                            vndr.ContactPerson,
                            vndr.Created_At,
                            vndr.ProfileUrl,
                            vndr.Url,
                            dept.Name
                        };

1 个答案:

答案 0 :(得分:1)

我们将首先做事:进行联接并创建一个您将返回的视图模型类。因为返回匿名对象并使用dynamic确实很麻烦。

已加入实体的ViewModel:

public class EmployeesViewModel
{
    public string BusinessAddress { get; set; } 
    public string BusinessDesc { get; set; } 
    public string BusinessEmail { get; set; } 
    /* ....all remaining properties */
}

然后我们将它们正确加入并选择为EmployeeViewModel

var employees = from vndr in context.vendorDomain
                join loc in context.Location on vndr.City equals loc.Id
                join bus in context.Business_Category on vndr.BusinessCategory = bus.BusinessId
                select new EmployeeViewModel
                {
                    BusinessAddress = vndr.BusinessAddress,
                    BusinessDesc = vndr.BusinessDesc,
                    BusinessEmail = vndr.BusinessEmail,
                    /* ... remaining properties here*/
               };

或者,如果需要方法语法:

var employees = context.vendorDomain
                .Join(context.Location,
                       vndr => vndr.City,
                       loc => loc.Id,
                       (vndr, loc) => new { vndr, loc,})
                .Join(context.Business_Category,
                       vndr_loc.vndr.BusinessCategory,
                       bus.BusinessId,
                       (vndr_loc, bus) => new {vndr_loc.vndr, vndr_loc.loc, bus})
                .Select(x => new EmployeeViewModel{
                    BusinessAddress = vndr.BusinessAddress,
                    BusinessDesc = vndr.BusinessDesc,
                    BusinessEmail = vndr.BusinessEmail,
                    /* ... remaining properties here*/
                });

根据您的评论,您需要在加入后打印vendorList。现在这很模糊,但是我假设您想将两者都提交给您的客户端/视图,因此,我们再次为其创建一个ViewModel类:

public class EmployeeVendorListViewModel
{
   public VendorList VendorList { get; set; }
   public EmployeeViewModel Employees { get; set; }
}

我们要做的最后一件事是将其粘合到您的ActionMethod中并返回:

[HttpGet]
public async Task<IActionResult> Get()
{
    //renamed using a lower case "v"
    var vendorList = await _vendorRepository.Query()
                         .Where(x => x.IsDeleted == false)
                         .ToListAsync();

    //the join from earlier. You should put it in a repo somewhere, so it does not clutter your controller
    var employees = from vndr in context.vendorDomain
                    join loc in context.Location on vndr.City equals loc.Id
                    join bus in context.Business_Category on vndr.BusinessCategory = bus.BusinessId
                    select new EmployeeViewModel
                    {
                        BusinessAddress = vndr.BusinessAddress,
                        BusinessDesc = vndr.BusinessDesc,
                        BusinessEmail = vndr.BusinessEmail,
                        /* ... remaining properties here*/
                    };
    //create the final view model and return it
    var vm = new EmployeeVendorListViewModel 
    {
        VendorList = vendorList,
        Employees = employees
    }

    return Ok(vm);
}

如果要在查询中使用NOLOCK,则必须将其包装在TransactionScope中。这已经在StackOverflow上得到了解答:NOLOCK with Linq to SQL