LINQ(C#)Join - 在单行中显示重复数据

时间:2016-11-29 10:55:26

标签: c# sql-server linq join

我是LINQ的新手。 我正在尝试加入表并查看并在数据表中显示结果。

考虑我的场景(数据库):1]

我的要求是在DataTable的UI上显示如下:2]

我曾尝试过以下左外连接(示例代码):`

var query = (

 from request in DB.Request
 join financeaprv in DB.DemoView on request.SurrogateID equals financeaprv.SurrogateID into financeaprvList
                             from financeaprv in financeaprvList.DefaultIfEmpty()
                             where financeaprv.ApproverType=="Finance"

  join sysadmin in DB.DemoView on request.SurrogateID equals sysadmin.SurrogateID into sysadminList
                             from sysadmin in legalaprvList.DefaultIfEmpty()
                             where sysadmin.ApproverType=="Admin"

  select new queryResult                             
  {
        FinanceApprv = financeaprv.ApproverName
        Admin = sysadmin.ApproverName

  }

`

但是没有成功。 任何人都可以告诉我什么是我的问题的LINQ查询.. 提前谢谢。

3 个答案:

答案 0 :(得分:0)

因此,您希望在匹配SurrogateId上组合Request表和DemoView表,并且您希望将结果列为RequestId,SurrogateId,Finance,Manager和Admin。

你的LINQ序列是:

Request.Join(DemoView,     // join the Request and DemoView tables
    r => r.SurrogateId,    // from each Request element take the SurrogateId
    d => d.SurrogateId,    // from each DemoView element that the SurrogateId
    (req, dem) => new                  // when they match, take the request element
    {                                  // and the demoView element
        RequestId = req.RequestId,     // and create a new object containing
        SurrogateId = req.SurrogateId, // the mentioned properties
        Finance = dem.Finance,
        Manager = dem.Manager,
        Admin = dem.Admin,
    });

答案 1 :(得分:0)

以下工作请尝试。

class Program
{
    static void Main(string[] args)
    {
        List<Request> lstRequest = new List<Request>();
        lstRequest.Add(new Request(1, 1));
        lstRequest.Add(new Request(2, 2));


        List<DemoView> lstDemoView = new List<DemoView>();
        lstDemoView.Add(new DemoView(1, "Ram", "Finance"));
        lstDemoView.Add(new DemoView(1, "Sam", "Manager"));
        lstDemoView.Add(new DemoView(1, "Dan", "Admin"));
        lstDemoView.Add(new DemoView(2, "abc", "Finance"));



        var query = (from request in lstRequest 
                     select new
                     {
                         RequestID = request.RequestID,
                         SurrogateID = request.SurrogateID,
                         Finance = lstDemoView.Any(x => x.SurrogateID == request.SurrogateID && x.ApproverType.ToLower() == "finance") ? lstDemoView.SingleOrDefault(x => x.SurrogateID == request.SurrogateID && x.ApproverType.ToLower() == "finance").ApproverName : "",
                         Manager = lstDemoView.Any(x => x.SurrogateID == request.SurrogateID && x.ApproverType.ToLower() == "manager") ? lstDemoView.SingleOrDefault(x => x.SurrogateID == request.SurrogateID && x.ApproverType.ToLower() == "manager").ApproverName : "",
                         Admin = lstDemoView.Any(x => x.SurrogateID == request.SurrogateID && x.ApproverType.ToLower() == "admin") ? lstDemoView.SingleOrDefault(x => x.SurrogateID == request.SurrogateID && x.ApproverType.ToLower() == "admin").ApproverName : ""
                     }).ToList();
    }
}

public class Request
{
    public int RequestID { get; set; }
    public int SurrogateID { get; set; }

    public Request(int RequestID, int SurrogateID)
    {
        this.RequestID = RequestID;
        this.SurrogateID = SurrogateID;
    }

}

public class DemoView
{
    public int SurrogateID { get; set; }
    public string ApproverName { get; set; }
    public string ApproverType { get; set; }

    public DemoView(int SurrogateID, string ApproverName, string ApproverType)
    {
        this.SurrogateID = SurrogateID;
        this.ApproverName = ApproverName;
        this.ApproverType = ApproverType;
    }
}

注意: - 不要在查询中多次使用DB.DemoView。只需一次收集列表对象并在查询中使用该列表。因此保存了多数据库调用。

答案 2 :(得分:0)

我想出了以下查询,它运作得非常好:

var query = (from request in DB.Request let approvers = DB.DemoView.Where(sid => sid.SurrogateID == request.SurrogateID).Select(col => new { col.ApproverName, col.ApproverType}) select new queryResult{FinanceApprover = approvers.Where(state => state.ApproverType.Contains("Finance")).Select(name => name.ApproverName).FirstOrDefault(),//and so on}

我根据我的要求故意使用.Contains()。 .Equals()会很好。