简体   繁体   English

C#实体框架联接,联接两个表时选择存在值

[英]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). 我想编写一种方法来检查数据库中是否已经存在一个值,该值可以存在于2个表中的1个中(与哪个表无关)。

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 ). 在注释区域/ ** /中,我要使用存在的值(可以是s.Domaina.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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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