Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-10
11
Medium Priority
?
363 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
[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
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 668 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 664 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
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.

 

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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