Solved

LINQ to ENTITIES  simple select multiple items??

Posted on 2011-02-16
13
2,026 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 62

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
 
LVL 62

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 62

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 62

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 62

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 62

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 62

Expert Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now