0

I need to execute SQL query with output parameter.

For example,

 SELECT @Count = COUNT(*) FROM dbo.SomeTable
 SELECT * FROM SomeTable WHERE Id BETWEEN 1 AND 10

After quering I need to know the @Count value. How can I do it with LINQ without using a stored procedure?

Thank you.

1 Answer 1

1
int value = yourDB.SomeTable.Count(q=>q.id >=1 && q.id <= 10);

linq is pretty easy :)


edit: so you want 2 items, the count, and then a limited part of the array.

        List<SomeTable> li = yourDB.SomeTable.ToList();
        int number = li.Count;
        List<SomeTable> partial = li.GetRange(0, 10);

or

        int value = yourDB.SomeTable.Count();
        List<SomeTable> partial = yourDB.SomeTable.ToList().GetRange(0, 10);

so the best LINQ thing for paging is:

        List<SomeTable> partial = yourDB.SomeTable.OrderBy(q=>q.id).Skip(0).Take(10).ToList();
Sign up to request clarification or add additional context in comments.

6 Comments

I understand, what you mean, but I have to implement paging. It is much more complex than I wrote SELECT * FROM SomeTable WHERE Id BETWEEN 1 AND 10 Is there a way to execute clean T-SQL code with output parameter?
Using your solution solution I have to do 2 queries to DB, but I want to male only one.
and what is the reason you want to make life difficult by swearing with only 1 query?
@SuperX: your two statements still result in two queries, even if you only use one execute statement in C#.
@Stefanvds: Are you sure you mean yourDB.SomeTable.ToList().GetRange(0, 10)? That would load the entire table into memory! Shouldn't you use yourDB.SomeTable.Where(q=>q.id >=1 && q.id <= 10).ToList() instead; or perhaps yourDB.SomeTable.OrderBy(q=>q.id).Take(10).ToList()
|

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.