riv
riv

Reputation: 93

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

Upvotes: 0

Views: 156

Answers (1)

Fals
Fals

Reputation: 6839

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;

Upvotes: 1

Related Questions