I'm trying to build a query to use for a search engine, that would look like this one:
SELECT * FROM sometable
WHERE col1 = 1
AND col2 = 2
AND (col3a = 3 OR col3b = 3 OR col3c = 3)
I though the code below would work:
SubSonic.Query query = new SubSonic.Query("sometable");
query = query.WHERE("col1", 1);
query = query.WHERE("col2", 2);
query = query.AND("col3a = " + 3).
OR("col3b = " + 3).
OR("col3c = " + 3);
but it doesn't as it results in something like this:
SELECT * FROM sometable
WHERE col1 = 1
AND col2 = 2
AND col3a = 3
OR col3b = 3
OR col3c = 3
How can I build a query I need?
The following should be pretty close to what you want, if OpenExpression/CloseExpression is supported in 2.0:
SubSonic.Query query = new SubSonic.Query("sometable");
.WHERE("col1", 1);
.AND("col2", 2);
.AND("col3a = " + 3).
.OpenExpression()
.OR("col3b = " + 3).
.OR("col3c = " + 3);
.CloseExpression()
I think you are meant to use WhereExpression/AndExpression/OrExpression to nest expressions reading the documentation but Ive never used it so cant say for sure. Try the below and see if it works
SubSonic.Query query = new SubSonic.Query("sometable");
query = query.WHEREEXPRESSION("col1", 1);
query = query.AND("col2 = " + 2);
query = query.ANDEXPRESSION("col3a = " + 3).
OR("col3b = " + 3).
OR("col3c = " + 3);
You can create two one query first that include all OR's after that from the resulting view..you can filter out AND condition..I dint check it out..but hope it will work...
I don't know SubSonic, but would this work?
query = query.AND("col3a = " + 3 + " OR col3b = " + 3 + " OR col3c = " + 3);
You could easily build that substring programmatically.
I see it's not as simple as I expected, at least not in the version I have so I decided to loop through the DataSet manually to filter out the records from the last check.
Unless there is a better way?
I would recommend you to upgrade to SubSonic 2.2... The new query functions added in 2.1 are much more powerful.
Related
I have a SQL query where I have to pass a string in my where, my string can have a simple quote in the name of the program and at the same time break the string and create an error in my request.
Yes I would just like to skip the code, but the actual logic has been done so that we are able to modify the code, so I can't just trust that.
Here is the query in my ASP.NET MVC 5 project:
IQueryable<ListeProgrammesCol> query = db.Database.SqlQuery<ListeProgrammesCol>(
"SELECT id AS offreID, nomProgramme AS nom, codeProgramme AS code, dateAjout, dateLastUpdate, gestionEnLigne " +
"FROM tbl_offreCol " +
"WHERE FK_etablissement = " + instId +" AND offreType = 3 AND archive = 0 AND codeProgramme = '" + code + "' AND nomProgramme = '" + progNom + "' " +
"ORDER BY nomProgramme").AsQueryable();
And here is the query if you want to text in SQL Server Management Studio:
SELECT
id AS offreID, nomProgramme AS nom, codeProgramme AS code,
dateAjout, dateLastUpdate, gestionEnLigne
FROM
tbl_offreCol
WHERE
FK_etablissement = 923000
AND offreType = 3
AND archive = 0
AND codeProgramme = '351.A0'
AND nomProgramme = 'RAC en Techniques d'éducation spécialisée'
ORDER BY
nomProgramme
This is the problem: d'éducation
//////UPDATE
I decided to use linq to make my request, so I no longer need to use quotes. Here is the query:
var query = (from oc in db.tbl_offreCol
where oc.FK_etablissement == instId
&& oc.offreType == 3
&& oc.archive == 0
&& oc.codeProgramme == code
&& oc.nomProgramme == progNom
select new ListeProgrammesCol
{
offreID = oc.id,
nom = oc.nomProgramme,
code = oc.codeProgramme,
dateAjout = oc.dateAjout,
dateLastUpdate = oc.dateLastUpdate,
gestionEnLigne = oc.gestionEnLigne
}).OrderBy(x => x.nom).AsQueryable();
I have this linq select statment and I am getting this erro {"error":"Null TypeMapping in Sql Tree"} back.
Ill put the code below but is there a way to get more information on what the issue is?
If you need the entities let me know and Ill copy them.
regards
var query = from o in _orderDataDbContext.tblSellFlangeOrders
join od in _orderDataDbContext.tblSellFlangeOrderDetails
on (int?)o.Id equals od.SellFlangeOrderId
join t in _orderDataDbContext.tblTypes
on od.TypeId equals (int?)t.Id
join l in _orderDataDbContext.VLegendeFlangeLengths
on od.LengthId equals (int?)l.Id
join g in _orderDataDbContext.VLegendeGrades
on od.GradeId equals (int?)g.Id
where o.Id == request._flangeOrderID
select new FlangeOrderDetailByPoCodeDto
{
SellFlangeOrderID = o.Id,
DetailID = od.Id,
Price = od.Price1000,
BF = od.Bf,
TypeID = t.Id,
Type = t.Inches1 + "x" + t.Inches2,
LengthID = l.Id,
Length = l.Length + "'" + l.OverLength + "\"",
GradeID = g.Id,
Grade = g.Grade
};
I had a similar issue after upgrading to .net core 3.1 in a code that was working.
The problem, in the end, was something like you have here:
Length = l.Length + "'" + l.OverLength + "\"",
It seems like you cannot concatenate int, decimal, etc... so you have to convert them into a string
Length = l.Length.ToString() + "'" + l.OverLength.ToString() + "\"",
In my node.js module, I have some data in an array which I need to construct a query:
var valueClause = "(fieldA = '" + data.fieldA + "' AND fieldB = '";
var whereClause = ' WHERE ';
var hasAdded = false;
data.accounts.forEach(function (account) {
whereClause += valueClause + account.fieldB + "') OR ";
hasAdded = true;
})
if (hasAdded) {
// remove OR
whereClause = whereClause.substring(0, whereClause.length - 3);
// use the whereClause in the query
...
}
At the end of the above codes, if I have 2 accounts, I have whereClause:
' WHERE (fieldA = 'abcde' AND fieldB = '0003') OR (fieldA = 'abcde' AND fieldB = '0002') OR
I always have to remove the last ' OR' bit.
Is there a smarter way to construct the above?
Since the value of fieldA seems to be fix in the loop you could write this query
WHERE fieldA = 'abcde' AND fieldB IN ('0002','0003')
Since you have no prepared SQL Statement, where you could use the Array directly, you have to join the values similar to yout approach.
If guaranteed at lest one value exists I concatenate in the loop always with leading comma and use substr(1) of the value
I am an asp.net web pages developer I am developing a website in which I need advanced sorting like www.olx.com which is a classifieds website but the problem is that I have more than 25 categories I am using SQL Server & have my tables broken into parts.
Now, as I have many categories so when i sort them for example if I search for samsung but at the same time I want to sort search data by price (high to low or low to high) & also at same time I want to filter data which has a description now I would need to make 100's of queries by using if's but is there a more convenient solution to this problem
Currently I am using this query:
sql = "SELECT * FROM users_table INNER JOIN response_table ON users_table.ID = response_table.ID LEFT OUTER JOIN miscellaneous_table ON users_table.ID = miscellaneous_table.ID LEFT OUTER JOIN response_table2 ON users_table.ID = response_table2.ID";
sql = sql + " where response_table.sub_category='" + incat + "'";
if (Request["search"] != "" && Request["search"] != null)
{
var search = Request["search"].Trim();
string[] querynew = search.Split(' ');
var searchquery = " and ";
foreach (string word in querynew)
{
searchquery += " users_table.adtitle LIKE '%" + word + "%' OR ";
}
sql = sql + searchquery.Remove(searchquery.Length - 4);
}
if (Request["min"] != "" && Request["min"] != null && Request["max"] != null && Request["max"] != "")
{
sql = sql + " and (CAST(response_table.price AS Float)) between " + Request["min"].Trim() + " AND " + Request["max"].Trim();
}
Thanks
You can use this short hand in the WHERE clause
Assuming your filter are #col1, #col2, #col3
SELECT ...
FROM ...
WHERE (#col1 = 0 OR col1 = #col1)
AND (#col2 = '' OR col2 = #col2)
AND (#col3 = 0 OR col3 = #col3)
So with the above where clause, if the user only supply filter for col2 then just pass 0 in for col1 & col2 and it will unfilter these.
Just a quick question about LINQ, I want to use a value from the returned dataset to lookup a value and return this. The line I am struggling with is .ViewingNotes = New Viewing(pt.ProspectId).GetViewings().Columns(7).ToString(). Is this possible?
With BusinessLayerObjectManager.Context
Return (From p As [Property] In .PropertySet
Join pt As Prospect In .Prospects On pt.Property.propertyID Equals p.propertyID
Where (p.Development.DevelopmentID = devId)
Select New DevelopmentList With {
.Apartment = p.propertyApartment + " " + p.Development.Name,
.PropertyId = p.propertyID,
.Client = pt.Client.clientFirstname + " " + pt.Client.clientLastname,
.ClientId = pt.Client.ClientID,
.ProspectiveDate = pt.prospectiveDate,
.ProspectiveStatus = pt.prospectiveStatus,
.Agent = pt.Client.userID,
.ViewingNotes = New Viewing(pt.ProspectId).GetViewings().Columns(7).ToString(),
.PropertyStatus = ""
}).ToList()
End With
Thanks in advance.
I suspect not, when the compiler tries to convert the 'Viewing(pt.ProspectId).GetViewings().Columns(7).ToString()' bit to SQL it will get very confused. You would need to do it in 2 stages.
Do the first Linq-to-entity select first returning just pt.ProspectId, with the .ToList() intact. Then use the results to do some more linq, linq-to-linq if you like, where you can do the lookup using a lambda.