简体   繁体   English

如果实体存在,则返回实体框架中的值

[英]If entity exists return a value in Entity Framework

I have Two tables "Customer" table and "Blacklist" customer table. 我有两个表“客户”表和“黑名单”客户表。 When i blacklist a customer i put the customerid as a foreign key to Blacklist table. 当我将客户列入黑名单时,我将customerid作为外键放入黑名单表。

What i want to do is I need to find the Customer by "CusId" in the Customer Table. 我想要做的是我需要通过客户表中的“CusId”找到客户。 I retrieve Name,Area,Telephone,Email from Customer table. 我从客户表中检索姓名,区域,电话,电子邮件。 When i retrive it, it should also check whether the customer id is in the black list customer table too. 当我检索它时,它还应该检查客户ID是否也在黑名单客户表中。 depending on the existance it should pass a boolean value. 根据它的存在,它应该传递一个布尔值。

Final result should have total 5 columns. 最终结果应该有5列。 (Name,Area,Telephone,Email,IsBlacklist). (名称,面积,电话,电邮,IsBlacklist)。

Please help me to code this Entity Framework C#. 请帮我编写这个实体框架C#。 Thanks in advance. 提前致谢。

Customer
---------
(CusId,Name,Telephone,Email)

Blacklist
---------
(CusId)

To start you off: 为了让你开始:

var customer =
    from c in Customer
    where c.CusId == yourId
    select new 
    {
        c.Name, c.Area, c.Telephone, c.Email, 
        IsBlacklist = Blacklist.Any(b => b.CusId == yourId)
    };

you can use navigation property of blacklist , that is exist on customer : 您可以使用blacklist导航属性,即客户存在的属性:

var customer = Customer.Select(u => new
{
    u.Name,
    u.Area,
    u.Telephone,
    u.Email,
    Blacklist = u.Blacklist.Any()
})
.ToList();

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

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