我的问题是如何加入具有相同ID但联系人不同的所选行
这是我正在做的输出
| Name | Address | Cellphone | Email |
| John | NY | n/a | johndoe@y.c |
| John | NY | 123456781 | n/a |
我希望我的输出是一个组合
| Name | Address | Cellphone | Email |
| John | NY | 123456781 | johndoe@y.c |
这是我的Linq
var an = (from a in db.Info
join b in db.Contact on a.ID equals b.InfoID
where b.ContactTypeID == 56
|| b.ContactTypeID == 59
select new
{
a.ID,
a.LastName,
a.FirstName,
a.MiddleName,
b.ContactTypeID,
b.Values
}).ToList();
List<InfoList> wlist = new List<InfoList>();
foreach (var row in an)
{
InfoList ci = new InfoList
{
ID = row.ID,
Name = row.FirstName + " " + row.MiddleName + " " + row.LastName,
ContactType = GetLookupDisplayValById(row.ContactTypeID),
ContactValue = row.Values
};
wlist.Add(ci);
}
return Json(wlist.ToList(), JsonRequestBehavior.AllowGet);
我希望有人能帮助我。
答案 0 :(得分:0)
对于该数据和仅该数据,自联接可行。这是你可以转换为linq的sql。
select name, address
, case when t1.cellphone <> 'n/a' then t1.cellphone else t2.cellphone end cellphone
, case when t1.email <> 'n/a' then t1.email else t2.email end email
from yourtable t1 join your table t2 on t1.name = t2.name
where t1.name = 'John'
但是,如果数据看起来像这样,
| Name | Address | Cellphone | Email |
| John | NY | 365418794 | johndoe@y.c |
| John | NY | 123456781 | n/a |
您想要什么样的输出?
答案 1 :(得分:-2)
select max(Name),MAX(Address),MAX(Cellphone),MAX(Email) from grp group by Name