How to express "is null" sql syntax in linq to sql - linq-to-sql

How can I express "IS NULL" sql syntax in Linq to Sql?
Where(r => (r.Level1.Equals(l[1] == "" ? null : l[1]))
In the above code linq to sql converts the linq expression into the following sql which is not what I want.
#p1=NULL
I want my linq to be converted into the following sql
#p1 is null
How can I achieve this?

Try this
if (l[1] == "")
Where(r => (r.Level1 == null));
else
Where(r => (r.Level1 == l[1]));
If you use the ternary operator the expression evaluator will find that it's a expression returning a string, cause of this it'll use the = operator.

Related

What is going wrong with this SSIS derived Expression

((DT_STR,20,1252)OccuranceRegion == "US" && ((DT_STR,20,1252)LegalEntity == "AECB" || (DT_STR,20,1252)LegalEntity == "FSB" || (DT_STR,20,1252)LegalEntity == "Both"))
? DATEADD("day",30,CapCreationDt) : (ISNULL(MaxofRevSCIAndSCI) ? (DT_DATE)"1900-01-01" : MaxofRevSCIAndSCI)
Whenever I put conditions as
OccuranceRegion == "US" AND
LegalEntity is null
The result is coming as NULL, I have also checked the value for MaxofRevSCIAndSCI, its not null. Why, it is not going in else part?
Any function of NULL (except the specialised, "NULL-killing" functions ISNULL and the like) evaluates to NULL. You only compare LegalEntity to string constants. These expressions will evaluate to NULL if LegalEntity is NULL. You need to replace the NULL values in LegalEntity with something else. Probably a derived column upstream
ISNULL(LegalEntity) ? "##NULL#' : LegalEntity
would be easier than doing this in the expression itself (because SSIS expressions are hideous to debug).

Yii criteria - modify to include OR Condition

I have the following line of code that does a simple query
if ($this->fulfilled)
$criteria->addCondition('fulfilled ' . (($this->fulfilled == 1) ? "IS NOT NULL" : "IS NULL"));
How can modify this to do a query that contains an OR command to basically run this query using the framework rather than raw SQL
if ($this->fulfilled)
AND (fulfilled is null OR fulfilled = 0)
Try something like that
$criteriaOr = new CDbCriteria();
$criteriaOr->addCondition('fulfilled IS NULL', 'OR');
$criteriaOr->addCondition('fulfilled = 0', 'OR');
Then if($this->fulfielled) {$criteria->mergeWith($criteriaOr);}

Keywords to make my Condition check correct in MS Access

I want a where condition in MS Access Query,
but i am not able to get that correct.
Here is the c# representation of my condition
If(vEmail==null)
return false;
else
return Email==vEmail
I want it to be translated into MS Access Query's where condition.
I dont know what are the keywords of MS Access query can be used here.
IIF(IsNull(vEmail), "False", vEmail) AS Email
This should do the trick. It says:
If vEmail Is Null (IsNull would result in true), then return "False", else return Email.
vEmail=Null Shouldn't work, because Null != Null. There are also a couple other ways you can check for null.
IsNull
Not IsNull
YourField Is Null
YourField Is Not Null

Is there a nesting levels limit with expressions used in SSIS packages?

Working in SQL Server 2008. My first stab at an SSIS script and I need to emulate some if/then conditional logic written in VB.net. I couldn't find any previous questions dealing with nested conditions in expressions and believe I'm following what I've been able to uncover via google on nested conditions in a derived column.
I'm receiving an error while attempting to use nested conditions in the derived column transformation editor. The error I'm receiving indicates that SSIS could not parse my expression. The actual exception: "Exception from HRESULT: 0xC0204006 (Microsoft.SqlServer.DTSPipelineWrap)"
Questions for which the answers might immediately answer my question (and create a new problem):
is there a nesting levels limit?
can nesting be performed in the condition1 portion of [expression] ? [condition1] : [condition2]
I'll give two snippets, the first is what I'm actually inserting, the second is a more reader-friendly version. Hopefully someone can point out my error.
Not sure that it has bearing, but please note that [BusArea] is a column derived in a previous step.
actual expression:
[BusArea] == "CCC" || [BusArea] == "NBU" || [BusArea] == "CA" ? (ISNULL([CASE_MORG]) or TRIM([CASE_MORG]) == "" ? ( ISNULL([TRX_MORG]) or TRIM([TRX_MORG]) == "" ? NULL(DT_WSTR,50) : [TRX_MORG]) : [CASE_MORG]) : (ISNULL([CASE_AGT]) or TRIM([CASE_AGT]) == "" ? ( ISNULL([TRX_AGT]) or TRIM([TRX_AGT]) == "" ? NULL(DT_WSTR,50) : [TRX_AGT]) : [CASE_AGT])
formatted for easier reading:
[BusArea] == "CCC" || [BusArea] == "NBU" || [BusArea] == "CA" ?
(ISNULL([CASE_MORG]) or TRIM([CASE_MORG]) == "" ?
( ISNULL([TRX_MORG]) or TRIM([TRX_MORG]) == "" ?
NULL(DT_WSTR,50)
: [TRX_MORG]
)
: [CASE_MORG]
)
: (ISNULL([CASE_AGT]) or TRIM([CASE_AGT]) == "" ?
( ISNULL([TRX_AGT]) or TRIM([TRX_AGT]) == "" ?
NULL(DT_WSTR,50)
: [TRX_AGT]
)
: [CASE_AGT]
)
I don't think there is any limit with nesting conditions. Even if there is one, I don't think we will reach the limit in the packages that we create handle our business processes.
You almost got everything correct. The issue with your conditional statement is that you have used or instead of ||
I copied your exact statement and pasted in Derived Transformation within a Data Flow task and got an error because the package couldn't parse the expression. I replaced all the or's with correct Logical OR (||) operator and the expression evaluated correctly.

LINQ subquery IN

I'm a newbie with the IQueryable, lambda expressions, and LINQ in general. I would like to put a subquery in a where clause like this :
Sample code :
SELECT * FROM CLIENT c WHERE c.ETAT IN (
SELECT DDV_COLUMN_VAL FROM DATA_DICT_VAL
WHERE TBI_TABLE_NAME = 'CLIENT' AND DD_COLUMN_NAME = 'STATUS'
AND DDV_COLUMN_VAL_LANG_DSC_1 LIKE ('ac%'))
How do I translate this in LINQ ?
var innerquery = from x in context.DataDictVal
where x.TbiTableName == myTableNameVariable
&& x.DdColumnName == "Status"
&& x.DdbColumnValLangDsc1.StartsWith("ac")
select x.DdvColumnVal;
var query = from c in context.Client
where innerquery.Contains(c.Etat)
select c;
from c in db.Client
where (from d in db.DataDictVal
where d.TblTableName == "Client"
&& d.DDColumnName == "Status"
&& dd.DdvColumnValLandDsc1.StartsWith("ac"))
.Contains(c.Etat)
select c;
If you are new to Linq, you absolutely need two essential tools. The first is a tool that converts most T-SQL statements to Linq called Linqer (http://www.sqltolinq.com/). This should take care of the query in your question. The other tool is LinqPad (http://www.linqpad.net/). This will help you learn Linq as you practice with queries.
I often use Linqer to convert a T-SQL query for me, and then use LinqPad to fine tune it.
Same example with Linq method syntax:
var innerquery = dbcontext.DataDictVal
.where(x=> x.TbiTableName == myTableNameVariable
&& x.DdColumnName == "Status"
&& x.DdbColumnValLangDsc1.StartsWith("ac"))
.select(x=>x.DdvColumnVal)
var query = dbcontext.Client
.where( c=>innerquery.Contains(c.Etat))
Note:
Am providing this answer, because when i searched for the answer, i couldn’t find much answer which explains same concept in the method syntax.
So In future, It may be useful for the people, those who intestinally searched method syntax like me today.
Thanks
karthik