"SELECT TOP " + this.mintPageSize + " * FROM tblGroupDetail WHERE GroupDetailId NOT IN " + "(SELECT TOP " + intSkip + " GroupDetailId FROM tblGroupDetail)")
How Convert this Query to Linq.SomeOne tell me with Code?i have tried
var innerQuery = (from fb in db.tblGroupDetails where fb.GroupDetailID select fb).Take(this.mintPageSize);
var result = from f in db.tblGroupDetails where innerQuery.Contains(f.GroupDetailID) select f;
I suspect you just need:
var query = db.GroupDetail
.OrderBy(...) // You really need an ordering
.Skip(intSkip)
.Take(mintPageSize);
You should work out what ordering you want, otherwise "the first N" doesn't have any meaning.
That's not a translation of your query so much as your intent - you should check what SQL it generates.
Related
i am not getting what is the issue in the query probably i am not following the correct way to put the string and char sign , i am inserting the data in c# to local host with where clause please check the query and Error i am getting
Here is the query
String insertQuery = "insert into exam_add (id,session_id,Title,From_date,To_date,class_id,is_Post,is_Lock) select '"+id+ ",s.session,'" + title.Text+",'"+ from.Value.Date.ToString("yyyy-MM-dd")+",'"+to.Value.Date.ToString("yyyy-MM-dd")+ ", c.class_name,'"+x+",'"+x+" from year_session s, classes c where s.id = '1' and c.id='" + cls + "'";
Exception image
here the image for exception i am getting after run this query
On your ...'"+x+"... you forgot to close the single quotes. You open them but you never close them after you add the X variable to your query. All SQL is seeing is "'0," which is invalid syntax.
I recommend use SQLparameters to avoid sql injection but your error is you forgot to close the single quotes it shoud be like this '"+cls + "'
String insertQuery = "insert into exam_add (id,session_id,Title,From_date,To_date,class_id,is_Post,is_Lock) select '" + id + "','"+s.session+"','" + title.Text + "','" + from.Value.Date.ToString("yyyy-MM-dd") + "','" + to.Value.Date.ToString("yyyy-MM-dd")+"' , '"+c.class_name+"','" + x + "','" + x + "' from year_session s, classes c where s.id = '1' and c.id='" + cls + "'";
I don't know why you need that on select columns. and you provided insufficient information and code on your question.
I'm looking a way to convert this query:
Select *
From Tasks
Where Addresses_Street1+' '+Addresses_Street2+' '+Addresses_City +' '+Addresses_ZipCode Like '%'+replace(ltrim(rtrim('Leon Deladriere 15')),' ','%')
to a Linq C# expression.
I know that Contains() and Trim() can be use for the method but how to handle the replace ' ' by '%' ?
This expression is used to provide to user one single input with an address and look into multiples columns to find a matching one.
I use Linqpad but I doesn't see equivalent in Linq
You can try.
(from r in Tasks where SqlMethods.Like(
r.Addresses_Street1 + " " +
r.Addresses_Street2 + " " +
r.Addresses_City + " " +
r.Addresses_ZipCode,
"%Leon Deladriere 15".Trim().Replace(" " , "%"))
select r)
Note, LinqSql is smart enough to know that it can do the Trim and Replace locally rather than asking Sql to do it.
However if you use a field rather than a constant, eg
(from r in Task where SqlMethods.Like( ... ,
r.Addresses_Street1 + " " +
r.Addresses_Street2 + " " +
r.Addresses_City + " " +
r.Addresses_ZipCode,
r.AnotherField.Trim().Replace(" " , "%"))
select r)
then this will ask sql to do the trim and replace.
Try this:
var user = "Leon Deladriere 15".Trim();
Tasks.Where(t => (t.Addresses_Street1 + t.Addresses_Street2 + t.Addresses_City + t.Addresses_ZipCode).Contains(user));
I have to use "messageId" and "parrentId" c# variables in sql query, but when I use error raise: "Incorrect syntax near '.2'.".
How can I use "messageId" and "parrentId" in below query?
internal DataTable getAllMessages(string messageId, string parrentId)
{
Query = "SELECT DISTINCT T1.* FROM mail_Reply T2 JOIN mail_Messages T1 ON (T2."
+ messageId + "=T1." + messageId + " OR T2." + parrentId + "=T1."
+ messageId + ")";
return ExecuteDataTable();
}
Thanks in advance.
Don't try and build a query string like that - it opens you up to a vulnerablity known as SQL Injection - and that is something you need to go away and read about right now...
Once you're done with that, read about Command objects - SqlCommand and friends...
Alternatively, consider embracing Entity Framework...
if you column names are like integer values 1,2,3, then try this,
Query = "SELECT DISTINCT T1.* FROM mail_Reply T2 JOIN mail_Messages T1 ON (T2.["
+ messageId + "]=T1.[" + messageId + "] OR T2.[" + parrentId + "]=T1.["
+ messageId + "])";
return ExecuteDataTable();
use string.format or build the query seperatly in a string variable and assign it to Query
I have a following SQL query that I run inside C# application. I work with local (no servers) database created in access:
string query = #"SELECT s.TagID, se.SessionID, '" +
DateTime.Now.ToString("MM/dd/yy HH:mm:ss tt") +
"' AS ScanningTime " +
" FROM (((Student s " +
" LEFT JOIN [CourseID-ModuleID] cm ON s.CourseID = cm.CourseID) " +
" LEFT JOIN [ModuleID-SessionID] ms ON ms.ModuleID = cm.ModuleID) " +
" LEFT JOIN [Session] se ON ms.SessionID = se.SessionID) " +
" WHERE s.TagID = #tagNo " +
" AND se.SessionDate = Date() " +
" AND DateAdd('n', -30, [SessionTimeStart]) < #timeNow " +
" AND se.SessionTimeEnd > #Plus30Min ";
Parameters and variables used in the query:
DateTime TimePlus = DateTime.Now.AddMinutes(30);
DateTime now = DateTime.Now;
string Plus30Min = TimePlus.ToString("hh:mm tt");
string timeNow = now.ToString("hh:mm tt");
command.Parameters.Add("tagNo", OleDbType.Integer).Value = tagNo;
command.Parameters.Add("Plus30Min", OleDbType.VarChar).Value = Plus30Min;
command.Parameters.Add("timeNow", OleDbType.VarChar).Value = timeNow;
At the moment, this query runs, but does not produce any results. However, if I delete the line:
" AND DateAdd('n', -30, [SessionTimeStart]) < #timeNow " +
Then the query runs perfectly. This means that there must be something wrong with this line inside the query. Can you see it somewhere? I looked at multiple websites for examples of date query criteria, but I cannot find the mistake, maybe you will be able to help. Thanks in advance.
The only thing I noticed is the ' sign surrounding n. Should i use quotation mark instead? If so, how can I achieve it inside the quotes?
Change your calling code to
DateTime now = DateTime.Now.AddMinutes(30);
and replace the offending line in query text with
" AND SessionTimeStart > #timeNow "
If you need a DateTime.Now somewhere in your code, you could easily obtain again from the same expression. However, I am a bit perplexed by your parameters. They works against Date/Time fields but you pass strings. If the above solution doesn't work try also to change the OleDbType.VarChar to OleDbType.DBTime or DbDate
EDIT Pay attention to the parameter order. You are using OleDB and the name of parameters is meaningless. You should insert the parameters in the parameter collection in the same exact order in which they appears in the query text. The #timenow and Plus30Min should be changed in position.
Your query ends to use the timenow parameter to test the SessioneEndTime and viceversa
command.Parameters.Add("tagNo", OleDbType.Integer).Value = tagNo;
command.Parameters.Add("timeNow", OleDbType.VarChar).Value = timeNow;
command.Parameters.Add("Plus30Min", OleDbType.VarChar).Value = Plus30Min;
Ok guys I think this is a pretty simple one, I just don't know the answer myself. I have this query which is as follows
var qry ="/tblEACNumbers?$filter = EACNumber eq " + x ;
The x is a string which is constantly chaning but the syntax requires the string which the query is using to filter must be in ' ' so this would work
var qry ="/tblEACNumbers?$filter = EACNumber eq 'Hello' ";
I understand I can change the string to get the first ' in by doing this
var qry ="/tblEACNumbers?$filter = EACNumber eq '" + x ;
But I don't know how to get the final ' after I have declared the + x string.
Any suggestions?
var qry ="/tblEACNumbers?$filter = EACNumber eq '" + x +"'";
or
var qry = String.Format("/tblEACNumbers?$filter = EACNumber eq '{0}'", x);
Well, you already know how to add string to a string, why not use that again?
var qry = "/tblEACNumbers?$filter = EACNumber eq '" + x + "'";
Another option is to use string.Format():
var qry = string.Format("/tblEACNumbers?$filter = EACNumber eq '{0}'", x);
Both of those options are vulnerable to injection attacks, so you should add some escaping, depending on where you send this query. You don't have to worry about this if x comes from a trusted source, though.