Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Linq. Count.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
shapper - 03 Mar 2008 13:55 GMT
Hello,

I have a table named Tags which is related to other two tables
ArticlesTags and FilesTags.

My linq query to select my tags would be something like:

var query = from t in db.Tags
                select new
                {
                    TagId = t.TagId,
                    TagText = t.TagText,
                    Active = t.FilesTags.Any Or t.ArticlesTags.Any
                }

What active specifies if a Tag is associated to a File or
Article.

Now I need to create a new query that retrieves 2 values:

1. Number of existing tags in table Tags.
2. Number of tags which are related to FilesTags or ArticlesTags.

I need to retrieve these 2 values to display in 2 ASP.NET labels.

Could someone, please, help me with this query?

Thanks,
Miguel
shapper - 04 Mar 2008 02:31 GMT
> Hello,
>
[quoted text clipped - 25 lines]
> Thanks,
> Miguel

Please, anyone?

Thank You,
Miguel
Marc Gravell - 04 Mar 2008 05:29 GMT
If I understand correctly, and using Northwind as a (hopefully similar
example), how about the below; note that it only does a single SQL
query (because we force the ToArray()), but to be honest, I wouldn't
shy away from 2 simpler selects - one for just the total Count(), and
one for the Count() with a Where()... anyway, see how this goes:

               var data = ctx.Customers.GroupBy(
                       cust => cust.CustomerCustomerDemos.Any() ||
cust.Orders.Any())
                       .Select(grp => new { Linked = grp.Key, Count =
grp.Count() }).ToArray();

               foreach (var row in data)
               {
                   Console.WriteLine("{0}: {1}", row.Linked,
row.Count);
               }
               Console.WriteLine("Total: {0}",
data.Sum(row=>row.Count));

There might be some other options... I'll see if anything occurs...

Marc
Marc Gravell - 04 Mar 2008 07:56 GMT
I found a better answer; the tricky part was getting
deferred execution, as normally Count() executes immediately.
(of course, you could just do 2 simple queries? one Count(),
one Where().Count())

I've approached this instead by using a projection from the
primary table, and used FirstOrDefault, which means that
even if there are no rows, an object with 0 in both counts
is returned.

Aside: note that the .Count(predicate) [even with the Expression
form] does a very funky looking query, hence the simpler
.Where(predicate).Count() usage.

Anyway, the C# is:

var counts = ctx.Customers.Select(x =>
   new
   {
       Total = ctx.Customers.Count(),
       Linked = ctx.Customers.Where(
           cust => cust.CustomerCustomerDemos.Any()
           || cust.Orders.Any()).Count()
   }).FirstOrDefault();

With the faitly reasonable-looking generated SQL:

SELECT TOP (1) [t5].[value] AS [Total], [t5].[value2] AS [Linked]
FROM (
   SELECT (
       SELECT COUNT(*)
       FROM [dbo].[Customers] AS [t1]
       ) AS [value], (
       SELECT COUNT(*)
       FROM [dbo].[Customers] AS [t2]
       WHERE (EXISTS(
           SELECT NULL AS [EMPTY]
           FROM [dbo].[CustomerCustomerDemo] AS [t3]
           WHERE [t3].[CustomerID] = [t2].[CustomerID]
           )) OR (EXISTS(
           SELECT NULL AS [EMPTY]
           FROM [dbo].[Orders] AS [t4]
           WHERE [t4].[CustomerID] = [t2].[CustomerID]
           ))
       ) AS [value2]
   FROM [dbo].[Customers] AS [t0]
   ) AS [t5]

I have looked at the query plan/IO, and it isn't doing anything /too/ crazy.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.