user2965164
user2965164

Reputation: 13

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)

Upvotes: 0

Views: 186

Answers (2)

Amir Sherafatian
Amir Sherafatian

Reputation: 2083

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();

Upvotes: 1

Douglas
Douglas

Reputation: 54887

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)
    };

Upvotes: 1

Related Questions