---
title: "LINQ to SQL Efficiency"  
description: "LINQ to SQL Efficiency"  
author: "Anonymous User"  
published: 2014-12-08  
updated: 2014-12-09  
canonical: https://www.mindstick.com/forum/12769/linq-to-sql-efficiency  
category: "asp.net"  
tags: ["linq", "sql server"]  
reading_time: 3 minutes  

---

# LINQ to SQL Efficiency

I am new to [LINQ](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries) and I have a [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) regarding a "Hit the [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) once" type of transaction.

In the below code I am databinding the [results](https://yourviews.mindstick.com/story/4497/usage-of-baking-soda-magical-results) of a [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) to a radio list. I want to run the query once, then work with the results before databinding. IE: If there are [values](https://www.mindstick.com/forum/327/sum-textbox-values), databind to the Radio list, otherwise show a [textbox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview) stating there are no values.

From my [online](https://www.mindstick.com/articles/13083/benefits-of-students-who-can-get-online-assignment) searches I have only found that I can run the query once with a .[count](https://www.mindstick.com/forum/157774/selecting-count-with-distinct)(), then run it again if the .count() is > 0.

I would prefer to hit the database once, then count the [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records), and proceed using the same resultset.

I was not sure of the terminology to use when searching, so please respond with the approprate terminology to use so that I can find the answer on my own!

```
using (RTOExceptionDataContext thisDataContext = new RTOExceptionDataContext()){    rdoSelectTransition.DataSource =    from tracking in thisDataContext.vw_RTOExceptionWorkflowTransitionMaps                                                where tracking.RTOExceptionId.Equals(Convert.ToInt32(Request.QueryString["RTOExceptionId"])) &&                                                tracking.RTOSecurityLevel.Equals((int)Master.thisUserSecurityLevel)                                                select new { tracking.RTOTransitionCd, tracking.TransitionDisp };    rdoSelectTransition.DataTextField = "TransitionDisp";    rdoSelectTransition.DataValueField = "RTOTransitionCd";    rdoSelectTransition.DataBind();}
```

## Replies

### Reply by Royce Roy

I did find an answer to my question! I am learning more about LINQ everyday, and I really love it! This allowed me to databind if there are results. Though the "else" is not shown below, it sets the visibility of the radio button to false.

```
        int thisUserSecurityLevel = (int)Master.thisUserSecurityLevel;        int thisUserSelectedException = Convert.ToInt32(Request.QueryString["RTOExceptionId"]);         using (RTOExceptionDataContext thisDataContext = new RTOExceptionDataContext())        {            var query = from tracking in thisDataContext.vw_RTOExceptionWorkflowTransitionMaps                        where tracking.RTOExceptionId.Equals(thisUserSelectedException) &&                        tracking.RTOSecurityLevel.Equals(thisUserSecurityLevel)                        select new { tracking.RTOTransitionCd, tracking.TransitionDisp };             if (query.Count() > 0)            {                 rdoSelectTransition.DataSource = query;                rdoSelectTransition.DataTextField = "TransitionDisp";                rdoSelectTransition.DataValueField = "RTOTransitionCd";                rdoSelectTransition.DataBind();             }                        }    }
```

### Reply by Anonymous User

You don't need to do this at all. Just keep your binding code exactly as it is and use the <EmptyDataTemplate> within the markup of the GridView to indicate what should be shown in the event that you bind an empty collection to the GridView.

If you're binding data to a type of control that doesn't support a feature like this, then the easiest option is to simply eagerly materialize the query into a collection and then get the size of that in-memory collection.

```
var data = (from tracking in thisDataContext.vw_RTOExceptionWorkflowTransitionMaps    where tracking.RTOExceptionId.Equals(Convert.ToInt32(Request.QueryString["RTOExceptionId"])) &&        tracking.RTOSecurityLevel.Equals((int)Master.thisUserSecurityLevel)    select new { tracking.RTOTransitionCd, tracking.TransitionDisp })    .ToList(); if(data.Any())    //databindelse   //do something else
```


---

Original Source: https://www.mindstick.com/forum/12769/linq-to-sql-efficiency

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
