Solved

Will this C# picece of code acts same as an SQL query?

Posted on 2010-11-10
11
351 Views
Last Modified: 2013-12-17
hello group,

In a code (following) it seems some data are retreived from a MS SQL 2008 database. Does this mean there no need for any query or stored procedure in the database physically?

I'm new to this type of coding...

Will this get the data?

Thanks!
var context = ObjectContextHelper.CurrentObjectContext;

            var query = from c in context.Countries

                        orderby c.DisplayOrder, c.Name

                        where (showHidden || c.Published) && c.Allowed

                        select c;

            var countryCollection = query.ToList();

Open in new window

0
Comment
Question by:akohan
11 Comments
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 34105501
You do not need to create a stored procedure if you are created your select statements in your code.
0
 
LVL 10

Assisted Solution

by:Tyler Laczko
Tyler Laczko earned 167 total points
ID: 34105510
it should say: select * from c in context.Countries
                        orderby c.DisplayOrder, c.Name
                        where (showHidden || c.Published) && c.Allowed
0
 
LVL 19

Assisted Solution

by:elimesika
elimesika earned 166 total points
ID: 34105512
HI

This is LINQ code that make you query collections in memory in the same manner you query databases.

For more info see
http://www.codeproject.com/KB/linq/UnderstandingLINQ.aspx
0
 

Author Comment

by:akohan
ID: 34105515

Do you mean this piece of code will do the job of retreiving data from table?
0
 

Author Comment

by:akohan
ID: 34105531

Elimesika,

Thanks for your help. I have added a new method to an existing code  (I just followed other methods to see how they work) my method now works but doesn't return any data off the database. so do you mean I need to do something with Linq?

Thanks.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:Zakabog
ID: 34105541
var query = from c in context.Countries
                        orderby c.DisplayOrder, c.Name
                        where (showHidden || c.Published) && c.Allowed
                        select c;

That looks like a query, does the code work? I'm not quite sure what you're asking.
0
 

Author Comment

by:akohan
ID: 34105714

yes it is a query but I'm new to this kind of query since I'm not famliar with linq
now, what I'm trying to say is that there is a methodA() uses this and gets the information so I have followed its step but mine doesn't bring back any data.
what could be missed in my work?


0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 167 total points
ID: 34106592
This is in fact a LINQ query, and is slightly different from issuing SQL statements by hand.
Technically, a LINQ query is used by .Net to search a group of objects, they don't necessarily have to be in a database.  For example, you can use a LINQ query to search a group of strings to find the ones that are more than 3 characters long:
string[] groupOfStrings = { "The", "quick", "brown", "Fox", "jumped" };
var query = from string s in groupOfStrings
                    where s.Length > 3
                    select s;
foreach (string selectedWord in query)
    Console.WriteLine(selectedWord);
When you add a LINQ-to-SQL class to your project Visual Studio writes a bunch of code for you behind-the-scenes, creating objects with properties that match fields in your tables, and adding methods to do the actual interaction with SQL server for you.  So the query in your question appears to be about equivelant to the SQL:
using (SqlConnection cnx = new SqlConnection("Your Connection String"))
{
  cnx.Open();
  using (SqlCommand cmd = cnx.CreateCommand())
  {
    cmd.CommandText = "SELECT * FROM Countries WHERE Published = True, AND Allowed = True ORDERBY DisplayOrder";
    using (SqlDataReader reader = cmd.ExecuteReader())
    {
      // Etc, etc
    }
  }
  cnx.Close();
}
(Behind the scenes, though, in the first case the LINQ query is running code similar to my longer second example)
Your query may not be returning anything because no records match the criteria.  Do you have records with Allowed = TRUE and is the variable showHidden = true?
0
 

Author Comment

by:akohan
ID: 34106994

Thanks for the information you shared, OK, I found out that I had forgotten to call the function in the caller !!! BUT now I'm getting an error as following:

Mapping and metadata information could not be found for EntityType 'Test-Solutions.BusinessLogic.Directory.CustomerType'

what causes this and how can I fix it?

Regards.
0
 

Author Comment

by:akohan
ID: 34107512


OK found the problem, there were two reasons:

1) My database model (TestModel.edmx) was not updated. However, after this step still there still was the same error message
2)The name of entity or table was not as the name in database... so I had to fix the naming but step 1 was requred anyway.

Thanks to all for your feedback.

0
 

Author Closing Comment

by:akohan
ID: 34488321
Sorry for the delay ... been out of town for a month with no access to internet!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

948 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

23 Experts available now in Live!

Get 1:1 Help Now