1

How can the foll. sql be written in Linq. I am using LinqToEntities,C#. @companyID & @branchID are the parameters

select * from tblEmp e
where e.deleted = 0 and
(e.companyId = @companyID OR e.companyid is null) and
(e.branchId = @branchID OR e.branchid is null)

For now, there is a stored procedure for the same and I am using it in linq, like this:

var qry = from d in MYDB.GetData(int companyid, int branchid)
select new
{
//all reqd. columns...
}

so, is it possible to write the above in Linq, directly.

EDIT:

public IEnumerable<tblEmp> GetData(Guid gCID, Guid gBID)
{
  var employees = (from e in tblEmp
                 where !e.deleted
                 && (e.companyId == gCID || e.companyid == null)
                 && (e.branchId == gBID || e.branchid == null)
                 select e
                 ).AsEnumerable();
}

//Both Parameters are '00000000-0000-0000-0000-000000000000'. I am expecting all records but the count is 0. Where am I going wrong.

2 Answers 2

2

Sure:

var employees = (from e in tblEmp
                 where !e.deleted
                 && (e.companyId == aCompanyIdHere || e.companyid == null)
                 && (e.branchId == aBranchIdHere || e.branchid == null)
                 select new
                 {
                     e.Id,
                     e.companyId,
                     e.branchId
                 });

Edit

The query should work, but I suspect you requirements are a bit different. If you want to get all items when a passed in parameter Guid is empty you could do the following.

public IEnumerable<tblEmp> GetData(Guid gCID, Guid gBID)
{
    var employees = (from e in tblEmp
                     where !e.deleted
                     && (gCID == Guid.Empty || e.companyid == gCID)
                     && (gBID == Guid.Empty || e.branchid == gBID)
                     select e
                     ).AsEnumerable();
}

But for better readability IMO:

public IEnumerable<tblEmp> GetData(Guid? gCID, Guid? gBID)
{
    var employees = (from e in tblEmp
                     where !e.deleted
                     && (gCID == null || e.companyid == gCID)
                     && (gBID == null || e.branchid == gBID)
                     select e
                     ).AsEnumerable();
}

and pass in null instead of Guid.Empty if you want all records.

var data = GetData(null, null);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Will this work if the datatype is guid instead of int. There are records in DB, but I get 0 count. I have updated my Question what I have tried.
@Ruby No problem, it really depends on your needs. I'm more concerned about the caller here :) GetData(Guid.Empty, Guid.Empty) vs GetData(null, null), but to each his own I guess :)
1
var qry = (
    from test in MYDB.tblEmp
    where !e.deleted
        && (e.companyId == "any id" || e.companyid == null)
        && (e.branchId == "anyid" || e.branchid == null)
    select test
).FirstOrDefault();

1 Comment

Perhaps you're accidentally right, but I don't see a TOP 1 in his query anywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.