1

I am creating a report in which i am creating a stored procedure , in that for one particular column I need to use following query

(SELECT COUNT(1) FROM [dbo].EmployeeDependent WITH (NOLOCK) 
 WHERE [dbo].EmployeeDependent.GCPDocumentId = gdh.GCPDocumentId ) 
     AS [No of Dependants travelling with employee] FROM tablename

and this whole subquery is used in the select clause of sql query. Now I want to convert this query to linq , but somehow i am not able to make it.

Thanks

5
  • EmployeeDependent.GCPDocumentId = + gdh.GCPDocumentId what does this line mean? Commented Jul 26, 2018 at 5:47
  • Sorry that query is ,(SELECT COUNT(1) FROM [dbo].EmployeeDependent WITH (NOLOCK) WHERE [dbo].EmployeeDependent.GCPDocumentId = gdh.GCPDocumentId ) AS [No of Dependants travelling with employee] FROM tablename Commented Jul 26, 2018 at 6:02
  • Basically you want result of select query in [No of Dependants travelling with employee] am i rit? Commented Jul 26, 2018 at 6:11
  • I recommend post your whole query Commented Jul 26, 2018 at 6:15
  • Try using this query: EmployeeDependent.Where(x => x.GCPDocumentId == gdh.GCPDocumentId).Count(); If this is a subquery, you need to include the value into parent query. Commented Jul 26, 2018 at 6:20

1 Answer 1

1

for the no lock you need to do the following:

using (var txn = new TransactionScope(
    TransactionScopeOption.Required, 
    new TransactionOptions
    {
        IsolationLevel = IsolationLevel.ReadUncommitted
    }
))
{

}

note: you are creating a new TransactionScope object and telling it to use a read-uncommitted isolation level. The query within the "using" statement now acts as if all its tables were reading with the NOLOCK hint.

your query will look like:

using (var txn = new TransactionScope(
    TransactionScopeOption.Required, 
    new TransactionOptions
    {
        IsolationLevel = IsolationLevel.ReadUncommitted
    }
))
{
    var count = from c in EmployeeDependent where c.GCPDocumentId = GCPDocumentId
    select new (DependantsTravelling = c.Count())
}
Sign up to request clarification or add additional context in comments.

Comments

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.