C# Linq Group By或Distinct with Join
我可能做得不对,但我有一个预订表和一个联系人表,两个表之间有一个连接表 我需要获取所有与包含给定字符串的电子邮件有相关联系的预订 换句话说,用户希望通过联系电子邮件搜索预订 这就是我想到的。它可以工作,但为每个预订返回重复的行。我还没有弄清楚如何对行进行分组,或者使用t-SQL中的不同方法C# Linq Group By或Distinct with Join,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我可能做得不对,但我有一个预订表和一个联系人表,两个表之间有一个连接表 我需要获取所有与包含给定字符串的电子邮件有相关联系的预订 换句话说,用户希望通过联系电子邮件搜索预订 这就是我想到的。它可以工作,但为每个预订返回重复的行。我还没有弄清楚如何对行进行分组,或者使用t-SQL中的不同方法 if (!String.IsNullOrWhiteSpace(form["contactemail"])) { string contactemail = form["contactemail"];
if (!String.IsNullOrWhiteSpace(form["contactemail"]))
{
string contactemail = form["contactemail"];
IList<int> bookingids = bookings.Select(x => x.bookingid).ToList();
IQueryable<contact> con = (from y in db.bookingscontacts where bookingids.Contains(y.bookingid) select y.contact);
//EDIT: I hadn't included the email filter...
IQueryable<contact> conmatches = (from c in con where c.email1.Contains(contactemail) select c);
IList<int> contactids = conmatches.Select(x => x.contactsid).ToList();
bookings = (from r in bookings from c in db.bookingscontacts where contactids.Contains(c.contactsid) && bookingids.Contains(r.bookingid) select r);
}
if(!String.IsNullOrWhiteSpace(表单[“contactemail”]))
{
字符串contactemail=格式[“contactemail”];
IList bookingids=bookings.Select(x=>x.bookingid.ToList();
IQueryable con=(从bookingid.contacts所在的db.bookingscontacts中的y(y.bookingid)选择y.contact);
//编辑:我没有包括电子邮件过滤器。。。
IQueryable conmatches=(从con中的c开始,其中c.email1.Contains(contactemail)选择c);
IList contactId=conmatches.Select(x=>x.contactsid.ToList();
bookings=(从数据库中c的bookings中的r开始,其中contactId.Contains(c.contactsid)和&bookingid.Contains(r.bookingid)选择r);
}
假设您有导航属性,否则您将开始使用它们:
var bookings = from b in bookings
where b.bookingscontacts
.Any(bc => bc.contact.email == contactemail)
select b;
这将生成一个
EXISTS
查询,因此预订不会重复。哦,天哪,我想我可能已经走远了。这太完美了-谢谢!事实上,似乎存在某种类型的初始bookings
表,并且结果也被称为bookings
,这可能会令人困惑。