?
Solved

LINQ to ENTITIES  simple select multiple items??

Posted on 2011-02-16
13
Medium Priority
?
2,034 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
[X]
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
  • 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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
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 2000 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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 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