Solved

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

Posted on 2010-11-10
11
350 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

15 Experts available now in Live!

Get 1:1 Help Now