Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

LINQ to ENTITIES  simple select multiple items??

Posted on 2011-02-16
13
2,030 Views
Last Modified: 2013-12-17
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?
0
Comment
Question by:mac-will
  • 7
  • 5
13 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34911736
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34911806
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
 
LVL 8

Author Comment

by:mac-will
ID: 34918716
@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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34918860
What happen when you run the test project against your database and NOT the test database?
0
 
LVL 8

Author Comment

by:mac-will
ID: 34919033
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34919166
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34919170
Place into zip files.
0
 
LVL 8

Author Comment

by:mac-will
ID: 34919289
https://filedb.experts-exchange.com/incoming/ee-stuff/8060-WindowsFormsApplication1.zip

This is a stripped down demo project that still has the issue...
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34919580
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
 
LVL 8

Author Comment

by:mac-will
ID: 34919723
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
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 34926268
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
 
LVL 8

Author Closing Comment

by:mac-will
ID: 34926706
Thanks for you help.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34926769
Not a problem, always happy to help.  ;=)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question