简体   繁体   中英

C# Entity Framework join, choose the exist value when joining 2 tables

I want to write a method that check if a value already exist in my database, the value can exist in 1 of 2 tables (doesn't matter which one).

This is relevant part in my code

using (Context db = new Context())
{
    var _domain = (from s in db.Subscriptions
                   join a in db.Alias on s.Id equals a.Subscription_Id
                   where (s.Domain == domain || a.Alias_Domain == domain)
                   select /*if s.domain exist take s.domain, if a.alias domain exist take a.alias*/).FirstOrDefault();

    return _domain != null ? 1 : 0;
}

In the comment area /**/ I want to take the value that exists (can be either s.Domain or a.Alias_Domain ).

Can someone please help me with that?

Thanks in advance

You can count the number domains:

var count = 
    (from s in db.Subscriptions
     join a in db.Allias on s.Id equals a.Subscription_Id
     where (s.Domain == domain || a.Allias_Domain == domain)
     select s).Count();

return count > 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM