Asp.net web api 在API中使用LAMBDA左连接以获得结果

Asp.net web api 在API中使用LAMBDA左连接以获得结果,asp.net-web-api,lambda,left-join,Asp.net Web Api,Lambda,Left Join,如何使用LAMBDA将下面代码中的连接实现为C Select VD.Id , VD.BusinessAddress , VD.BusinessDesc , VD.BusinessEmail , VD.BusinessName , VD.BusinessZip , VD.ContactPerson , VD.ContactNo , VD.ProfileU

如何使用LAMBDA将下面代码中的连接实现为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);

    }

我们将首先做一些事情:进行连接并创建一个您将返回的视图模型类。因为返回匿名对象并使用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 = 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*/
                });
根据您的评论,您需要在加入后打印供应商列表。这一点很模糊,但我假设您希望将两者提交给客户机/视图,因此我们再次为其创建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上已经回答了这个问题:

您只是想将sql查询转换为linq,还是想以某种方式将其与VendorList变量合并?@Marco我需要再次将城市名称和业务类别添加到VendorList表中:您只需要将sql查询转换为linq查询,还是需要更多-例如,以某种方式使用VendorList变量?我必须在Join之后打印VendorList。还想问一下,是否所有这些都可以用Lambda表达式来完成?我已经更新了我的答案。不过,我个人会选择查询语法。它更具可读性和简洁性。很高兴我能提供帮助。如果您能帮助我,我还需要一点代码方面的帮助?只需问一个新问题就可以了。这就是一切;
public class EmployeeVendorListViewModel
{
   public VendorList VendorList { get; set; }
   public EmployeeViewModel Employees { get; set; }
}
[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);
}