简体   繁体   中英

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.

What i want to do is I need to find the Customer by "CusId" in the Customer Table. 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. depending on the existance it should pass a boolean value.

Final result should have total 5 columns. (Name,Area,Telephone,Email,IsBlacklist).

Please help me to code this Entity Framework 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 :

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

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