LINQ to ENTITIES simple select multiple items??

Hello,

I'm very new to LINQ and LINQ to Entities.

I am testing a simple example and I can't seem to figure out what is going on.

Here is the code snippet:
using (NH_Ats_v2Entities db = new NH_Ats_v2Entities(entityBuilder.ToString()))
            {     
                var query = from t in db.testresults_TestInfos
                    where t.AllTestsPassed == true
                            select t;

                int tot = query.AsQueryable().Count();

                foreach (var test in query)
                {
                    Console.WriteLine("Test Id: {0}", test.TestId.ToString());
                    Console.WriteLine("Part Number: {0}", test.PartNumber);
                    Console.WriteLine("Sales Order: {0}", test.SalesOrder);
                    Console.WriteLine("");
                }
            }

Open in new window


The issue:  when the above executes the value of "tot" is 2 as expected from the SQL database.  However the foreach loop executes several times printing the same 2 records about 10 times each.

What is going on?
LVL 8
mac-willAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi Mac;

It does not look as if you were doing anything wrong. I believe it might of been some corrupt solution files. On what way you should go read this blog it may give you some insight on the way to go.

Linq-to-SQL vs Linq-to-Entities : Revisited
http://naspinski.net/post/Linq-to-SQL-vs-Linq-to-Entities--Revisited.aspx

Fernando
0
 
Fernando SotoRetiredCommented:
Hi mac-will;

That should not be happening and difficult to troubleshoot without analyzing the code while it is running. Can you put a sample project with a test database together that shows the problem and zip the files up and post or if you can zip the current project and test database. If you can please follow these instructions.:

To upload a file in zip format to the Experts Exchange Stuff web site at http://www.ee-stuff.com follow these steps:

1.  Zip the files or project to be uploaded
2.  The go to http://www.ee-stuff.com
3.  If you are prompted to log in use the same username and password you use on the main site here
4.  At the top of the page click on "Expert Area" tab
5.  Then click on "Upload a new file" link
6.  Follow the instructions on the page
7.  After a successful upload post the link of the
    file in the question.

Fernando
0
 
Carl TawnSystems and Integration DeveloperCommented:
It's possibly something to do with the fact that you are executing the query twice. Try commenting out the line:
int tot = query.AsQueryAble().Count();

Open in new window

and seeing if that has any impact.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
mac-willAuthor Commented:
@carl_tawn:  Commenting out that line has no affect.

@FernandoSoto:  I'm not sure I can easily zip the project.  I tried making a test database and project but it did not have the same problem.

I will see if I can upload something.
0
 
Fernando SotoRetiredCommented:
What happen when you run the test project against your database and NOT the test database?
0
 
mac-willAuthor Commented:
The probelm does not happen when I run the test project against the original database.

==> It has something to do with the model in my original project.  If I delete certain tables in the model than I get a different number of printouts.  Some sort of circular reference...
0
 
Fernando SotoRetiredCommented:
Can you upload the EDMX files, ModelName.edmx and ModelName.Designer.cs so I can see what the model looks like. The Model that does not work.  ;=)
0
 
Fernando SotoRetiredCommented:
Place into zip files.
0
 
mac-willAuthor Commented:
https://filedb.experts-exchange.com/incoming/ee-stuff/8060-WindowsFormsApplication1.zip

This is a stripped down demo project that still has the issue...
0
 
Fernando SotoRetiredCommented:
Seeming that you are using the parent table and the other three tables are not involved in the query this is what I would try.

1. In the IDE select Build from the main menu bar and click on Clear Solution then rebuild the solution and try it.
2. If that did not work Create a new solution and import your data model and source code files and try it there. It may be that the old solution files may be corrupted and causing the problem.
0
 
mac-willAuthor Commented:
Clear Solution?  Do you mean "Clean Solution"?  Tried that several times...

I think I will just start the model over.  I really just wanted to make sure I was doing something wrong.

As a sidenote - I'm still not sure if I should go with LING - Entities or LINQ - SQL.  This is still an evaluation phase for me.

Thanks

Mac
0
 
mac-willAuthor Commented:
Thanks for you help.
0
 
Fernando SotoRetiredCommented:
Not a problem, always happy to help.  ;=)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.