Solved

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

Posted on 2010-11-10
11
352 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET (2008) - Refactoring Question 2 19
Service Controller 1 23
Alert on Server memory 2 20
Help with consolidating excel files using VB.net 2 21
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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