how to implement and, or clauses in sql queries? ruby on rails - ruby

I tried looking around but could not find an answer on how to implement the combination of AND, |(or). I am getting an error. The code is listed below:
allgames = GameInfo.Get.where(["list = ? AND (lang = ? OR lang = ?)", "yes", "en", #lang]).order(:order)
The error I am getting is below:
The specified query expression syntax is not valid.
please shed some light,
Regards,

Pipe is not used in sql:
where("list = ? AND (lang = ? OR lang = ?)", "yes", 'en',#lang)

Related

How to check "Is Nothing" in Linq dynamic query

I'm trying to check a very basic expression with Linq dynamic queries.
Although this woks perfectly with regular Linq.
Dim xxx = From x In db.Users Where x.AgentID Is Nothing Select x.FirstName, x.LastName
it seems not to work with dynamic queries:
Dim xx = db.Users.Where("AgentID Is Nothing").ToList
I get this error:
Boolean expression expected
What am I doing wrong ?
You must have a Boolean expression not a string to get the result like:
Dim xx = db.Users.Where(u=>u.AgentID==null).ToList()
I found following solution :
"AgentID.HasValue = False"

How do I execute a query that returns true or false if a row exists?

I'm familiar with creating queries like this in my code:
var word = dbcon.Query<Word>("SELECT Correct FROM Phrase WHERE Id = ?", id).FirstOrDefault();
But is there a way I could execute this query and have it return just a true or a false if the value existed or not?
"SELECT Correct FROM Phrase WHERE Id = ?"
Note that I am using SQLite.net so I would be interested in solutions that use that.
You should be able to use the Any() extension method, so like:
var word = dbcon.Query<Word>("SELECT Correct FROM Phrase WHERE Id = ?", id).Any();

Linq where clause invalid

var advocacy = (from c in svcContext.CreateQuery("lead")
join a in svcContext.CreateQuery("product")
on c["transactioncurrencyid"] equals a["transactioncurrencyid"]
where a["capg_billingtimeframe"].Equals(126350000)
select new
{
dues = c["capg_calculatedduesbilling"],
product = c["capg_primaryproduct"],
listprice = a["price"],
eligibility = c.FormattedValues["capg_eligibility"]
});
That is my linq query and it is giving me the error: Invalid 'where' condition. An entity member is invoking an invalid property or method.
I have looked online everywhere and done their suggestions. I am not using Xrm.cs because late binding can be faster. I have tried using the == operand and I have tried doing (int) and Convert.ToInt32(a["capg_billingtimeframe"]) and even converting everything to a string. I will say that a["capg_billingtimeframe"] I know is an object (that's why I did those conversions.
I'm guessing by that integer that capg_billingtimeframe is an optionset. If it is, you need to cast it like this:
where ((OptionSetValue)a["capg_billingtimeframe"]).Value == 126350000
I used early bound and for getting the local I wrote:
OptionSetValue branch = this.InputTargetEntity.Attributes.Contains("capg_calculatorutilized") ? (OptionSetValue)this.InputTargetEntity["capg_calculatorutilized"] : (OptionSetValue)this.TargetPreImage["capg_calculatorutilized"];
I then had to get the Optionsets.cs using crmsvcutil and writing:
if (branch.Value == (int)capg_calculatorrequired.SectionA)
Works like a charm.

? in Doctrine query means?

i wonder what this query means:
$blogPost = Doctrine_Query::create()->from('BlogPost p')->where('p.slug = ?', 'doctrine-released')->execute();
could someone explain the ? for me?
thanks
I am guessing, but I would bet money that the ? is just a way of saying 'there is a variable here and I will later populate it', just like normal binding in other SQL varieties. In your example, that would imply that the ? is expanded to 'doctrine-released' at execute time. In other words, the query becomes where p.slug = 'doctrine-released'
If there is a variable as the parameter then the '?' is used other wise '?' is not needed.
For example:
$blogPost = Doctrine_Query::create()->from('BlogPost p')->where('p.slug = ?', $doctrine-released)->execute();
and in case of a string as a param
$blogPost = Doctrine_Query::create()->from('BlogPost p')->where('p.slug' = 'doctrine-released')->execute();

LINQ Dynamic Expression API, predicate with DBNull.Value comparison

I have an issue using the Dynamic Expression API. I cannot seem to compare a DataTable field against DBNull.Value. The API is supposed to be able to "support static field or static property access. Any public field or property can be accessed.". However given the following query:
var whatever = table1.AsEnumerable()
.Join(table2.AsEnumerable(),
(x) => x.Field<int>("Table1_ID"),
(y) => y.Field<int>("Table2_ID"),
(x, y) => new { x, y})
.AsQueryable()
.Where("x[\"NullableIntColumnName\"] == DBNull.Value");
I end up getting the error: "No property or field 'DBNull' exists in type '<>f__AnonymousType0`2'"
Anyone have ideas on how to get around this? I can't use Submission.Field("NullableIntColumnName") in the string passed to the Where method either, btw, or else I would be able to compare against null instead of DBNull.Value.
Well, I finally got it. cptScarlet almost had it.
var values = new object[] { DBNull.Value };
...
.Where("x[\"NullableIntColumnName\"] == #0", values);
or
.Where("x[\"NullableIntColumnName\"] == #0", DBNull.Value);
What happens when you replace your current .Where with something like
.Where(string.format("x[\"NullableIntColumnName\"] == {0}",DBNull.Value));
If you change x.Field<int>("Table1_ID") to x.Field<int?>("Table1_ID") then you'll get nullable integers instead of regular integers, and any DBNull values will be converted to simple C# null values. Based simply on your code snippet, I'm not even sure you'd need dynamic expressions - a simple .Where(foo => foo.x == null) ought to work.
In general, you can also try:
.Where("NullableColumnName.HasValue");
Sorry to non-answer with a USL but...
Have you looked in the source? There's not a lot of it. My guess is that DBNull is not in the list of registered root objects.
I dont have the source to hand right now, but it is also likely to tell you what any other constants one might compare against might be.
.Where(a => a.IntColName == null);
Edit:
Sorry, I did't see this dynamic requirement... Dynamic would be: (at least in Framework 4)
var intColName = "...";
.Where(string.Format("it.{0} is null", intColName));

Resources