[英]Linq Group By or Distinct with Join
我可能不正確地處理此問題,但是我有一個預訂表和一個聯系表,它們之間有可連接的連接。
我需要獲取所有包含具有給定字符串的電子郵件的相關聯系人的預訂。
換句話說,用戶希望通過聯系電子郵件搜索預訂。
這就是我想出的。 它有效,但是每次預訂都返回重復的行。 我無法弄清楚如何對行進行分組或像在T-SQL中那樣使用不同的方法...
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);
}
讓我假設您具有導航屬性,否則將開始使用它們:
var bookings = from b in bookings
where b.bookingscontacts
.Any(bc => bc.contact.email == contactemail)
select b;
這將生成一個EXISTS
查詢,因此不會重復預訂。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.