Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

LINQ to ENTITIES  simple select multiple items??

Posted on 2011-02-16
13
Medium Priority
?
2,039 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 64

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 64

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 64

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 64

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 64

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 64

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 64

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

581 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