473,379 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

finding out if a linq table has 0 rows

How would you find out if a linq table has 0 rows in it? I have this code:

NewsContext.V_News() '*** linq table to be tested for 0 rows

Any ideas?

Nov 19 '08 #1
2 3743
linq to sql queries are only executed when you foreach the results. as a
datareader is used the row count is not known until all rows are read.
so the easiest is convert the query to an array. you then have both the
data and the row count (array size) at that point.

if you just need a row count, then don't use linq, as it has to read all
the rows and count them. add a method to the context that uses a
sqlcommand and count(*) to get the rowcount. sqlserver in this case will
do the count much faster.

note- in linq to sql, every time you foreach the query, its run again

-- bruce (sqlwork.com)

Andy B wrote:
How would you find out if a linq table has 0 rows in it? I have this code:

NewsContext.V_News() '*** linq table to be tested for 0 rows

Any ideas?
Nov 20 '08 #2
"linq to sql queries are only executed when you foreach the results."
IEnumerable. Makes sense.

"as a datareader is used the row count is not known until all rows are
read."
Does linq make extensive use of dataReader internally?

"so the easiest is convert the query to an array."
Will keep it in mind for later if I need it.

"you then have both the data and the row count (array size) at that point."
Good way to do it if you need the results and the count. Doesn't linq have
something like this:
'*** query the v_news linq table
NewsContext.CreateQuery("select count(*) from v_news")

"if you just need a row count, then don't use linq, as it has to read all
the rows and count them. add a method to the context that uses a sqlcommand
and count(*) to get the rowcount. sqlserver in this case will do the count
much faster."
I think I will use this instead. Just trying to prevent a trip to the server
just for that.

"note- in linq to sql, every time you foreach the query, its run again"

Except when using data functions that are linked to sql procedures and
functions.
Nov 20 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jrett | last post by:
I'm new to ASP.NET and fairly inexperienced with web development in general, but I've been a professional software dev for over 10 years, C++, Unix and windows, C# the past 4 years. I've been...
4
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). ...
8
by: Andy | last post by:
Hi, I'm trying to add a where clause to my query: List<stringtypes = new List<string>(); types.Add( "A" ); types.Add( "B" ); query = query.Where( c =types.Contains( c.Type ) );
1
by: ph_haenggi | last post by:
Hi folks I am pretty new to linq and in one of my first samples I tried to do the following: In my sample-database I have a table which has some columns and one of them has the type 'xml'. ...
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
21
by: hrishy | last post by:
Hi Will LINQ be ported to Python ? regards Hrishy
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
2
by: jelle79 | last post by:
Hi all, I'm storing all kind of data stored in objects. Now I want to query my data-source. And I thought LINQ is the right thing for it. Data is stored like: store.Database Queries like...
0
Sepidar
by: Sepidar | last post by:
Hi, I have a Customers table in my database with a corresponding ExpectedAddresses table that holds expected addresses of each Customer. Rows of later table are constant for each instance of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.