使用linq进行多个连接



这是我的查询:

var results = from table1 in dtSplitDates.AsEnumerable()
                      join table2 in dtSplitDates.AsEnumerable() on (int)table1["FID"] equals (int)table2["FID"] into lj
                      from r in lj.DefaultIfEmpty()
                      select dtSplitDates2.LoadDataRow(new object[]
                        {
                            r["FID"],
                            r["SLNO"],
                            r == null ? string.Empty : r["Dates"]
                        }, false);

目前我正在加入列FID -由于我得到36条记录(重复):

然而,为了避免重复,我需要在SLNO列上加入,但我无法编写该查询-请帮助。

根据我的理解,您需要两个连接条件;试试这个

   var results = from table1 in dtSplitDates.AsEnumerable()
                  join table2 in dtSplitDates.AsEnumerable() 
                  on new {id1 =(int)table1["FID"], SLno1= (int)table1["SLNO"]} 
                  equals new {id2=(int)table2["FID"], SLno2=(int)table2["SLNO"]}  into lj
                  from r in lj.DefaultIfEmpty()
                  select dtSplitDates2.LoadDataRow(new object[]
                    {
                        r["FID"],
                        r["SLNO"],
                        r == null ? string.Empty : r["Dates"]
                    }, false);

尝试用这个例子实现:

For Multiple Joins:

var result=(from com in db.Company.AsEnumerable()
                    join c in db.Country.AsEnumerable() on com.CountryID equals c.CountryID
                    join s in db.State.AsEnumerable() on com.StateID equals s.StateID
                    join ct in db.City.AsEnumerable() on com.CityID equals ct.CityID
                    orderby com.Name
                    select new CompanyModel()
                     {
                         CompanyID = com.CompanyID,
                         Name = com.Name,
                         AddressLine1 = com.AddressLine1,
                         CountryID = com.CountryID,
                         StateID = com.StateID,
                         CityID = com.CityID,
                         Country = c.CountryID,
                         State = s.StateID,
                         City = ct.CityID,
                         Pin = com.Pin,
                         Phone = com.Phone,
                     }).Distinct().ToList();

相关内容

  • 没有找到相关文章

最新更新