1

I want to use First() in a Linq2sql query, but I don't want the database to return the whole row, just specific columns. Is there a way to do that?

If I use Where() I can then Select() (while it stays an IQueryable), but if I use Where() it will iterate over all rows instead of stopping when it finds a match.

9
  • SELECT ColumnName FROM Table? Commented Dec 3, 2018 at 15:29
  • @Sami I just now edited the question in case someone doesn't notice the linq-to-sql tag. Commented Dec 3, 2018 at 15:30
  • Do you want a linq answer or an SQL answer? Commented Dec 3, 2018 at 15:30
  • @jarlh According to the edit and the comment, he look for a linq answer Commented Dec 3, 2018 at 15:31
  • Write Where().Select() first and then use First(). That's the equivalent of SELECT TOP 1 col1,col2 ..... WHERE .. Commented Dec 3, 2018 at 15:47

1 Answer 1

1

when you use "Where" it does not iterate over all record until you actually execute the query. because thats how linq works, until you actually use the result of the query (convert it to a list, bind to a gridview, etc...) it is not executed so you can add as many conditions as you want and they will combine into a single query.

so where.select.first should work

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.