sql to linq in c# code

Posted on 2014-01-25
Medium Priority
Last Modified: 2016-02-10
            SqlCommand cmsql;
            SqlDataReader drsql;

            string sql = "select max(patientid)as patientid from patient";

            cmsql = new SqlCommand(sql, cn);

            drsql = cmsql.ExecuteReader();
            if (drsql.Read())
                    int pid = Convert.ToInt16(drsql["patientid"].ToString()) + 1;
                    txtpatientid.Text = pid.ToString();



                txtpatientid.Text = "1";

Open in new window

this is my sql code how i make it in linq

my table name is== patient
Question by:loveuajay
  • 3
  • 2
LVL 12

Expert Comment

by:Habib Pourfard
ID: 39808691
using (MyDataContext dc = new MyDataContext())
    int pid = dc.patients.Max(t => t.patientid);

Open in new window

LVL 64

Accepted Solution

Fernando Soto earned 2000 total points
ID: 39808754
Hi loveuajay;

The following code snippet will do the same as your SQL code in the question.

// Create a Linq to SQL DataContext 
var db = new DataContextType();
Int16 maxPatientId = 0;

// Get the max ID from the patient table 
if( db.patient.Count() != 0 )
    maxPatientId = (Int16) db.patient.Max( p => p.patientid );

txtpatientid.Text = (++maxPatientId).ToString();

Open in new window


Author Comment

ID: 39809691
i get this error

Error      1      The type or namespace name 'DataContextType' could not be found (are you missing a using directive or an assembly reference?)      D:\ajay\jolly tech project\jollytech p1\jollytech p1\patient.cs      79      26      jollytech p1
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

LVL 64

Expert Comment

by:Fernando Soto
ID: 39810106
Hi loveuajay;

When working with Linq to SQL the first thing you need to do is create your data context and in Linq to SQL it is creating a EDMX which is Object Relational/Model, This model defines the tables in the database and the relationships between them. I would suggest reading the following web link which describe what LinQ to SQL is and how to create the OR/M, EDMX file and querying the database.

Using Linq to SQL Part 1

Once you have created the EDMX file which maps the database to the classes in your code that is what you will use for the variable DataContextType in the code snippet I posted.

Author Closing Comment

ID: 39811550
thanks i get it
LVL 64

Expert Comment

by:Fernando Soto
ID: 39812068
Not a problem, glad to help.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

607 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