Problem with LINQ Where Clause - linq

var results = from formNumber in context.DetailTM
join c in context.ClaimPeriodTM on formNumber.ClaimPeriod equals c.Cid
where formNumber.FormNumber.StartsWith(fNumber)
&& formNumber.RegistrationNumber != registrationNumber
select new { RegNo = formNumber.RegistrationNumber,
CP = c.ClaimPeriod, FormNo = formNumber.FormNumber };
The AND CLAUSE with .StartsWith Doesn't work. If I use == operator the query works fine. I tried adding brackets to the where clause but it didn't help. Any idea what is missing. Thank you in Advance.

Try something very basic like "&& true == false" and see if that fails. Also reverse the order. If you reverse the order, is it still the second clause that fails?
There's nothing about StartsWith that should cause this behavior. There's something else going on here. Trim and move elements until you isolate the real bug.

What is the database type for FormNumber? If its char (and not varchar), then you may have to trim one or both sides of the comparison.
where
formNumber.FormNumber.Trim().StartsWith(fNumber.Trim())
&& formNumber.RegistrationNumber != registrationNumber

Same as jrummells answer, just targetted by the additional comments.
formNumber.RegistrationNumber != registrationNumber
Check the database types here. If they are fixed length (char or nchar), you may need to do some trimming.

Related

BASIC IF THEN using Like

I am currently trying to write an IF/THEN statement in BASIC but none of the 6 relational Operators seem to help me. I want to compare a variable (srName) and if it contains the letters TS I want the relevant statements to play out.
Just to add the bit of the code below for anyone that wants to see it. I have currently left LIKE in there to show what I want it to do. I know it won't work as is with that condition. The only line that is broke is the first one.
IF srName LIKE '%TS%' THEN
status = ChangeSpec(result, 'T')
return newDate
ELSEIF srName LIKE '%SD%' THEN
status = ChangeSpec(result, 'F')
return newDate
ELSE
return ""
ENDIF
As the code is I get an error stating i am missing an ENDIF which I clearly am not. And the reason i know the problem is specifically with the part
LIKE '%xx%'
is because when i remove that with a simple
IF (srName = TSA245) THEN
which for testing I have forced my variable to match then the code works fine
OK I have managed to solve it. LIKE won't work in any configuration but instead i used the InStr function like below. Hope this helps others.
IF (InStr(srName, "TSA") = 1) THEN
status = ChangeSpec(result, 'T')
return newDate
ELSEIF (InStr(srName, "SDA") = 1) THEN
status = ChangeSpec(result, 'F')
return newDate
ELSE
return ""
ENDIF
Note: This works because i know my variable srName will begin with TSA or SDA. If you don't know where in the string it will be use > 0 instead of = 1

How write a hash function to make such expression to be true?

pseudocode:
// deprecated x!=y && hash(x) == hash(y) // how to make this true?
x!=y && hash(x) == hash(y) && (z!=x && z!=y) && (hash(x) != hash(z) && (hash(y) != hash(z)) // how to make this true?
x and y can be any readable value
Whatever the language, the pseudocode is just help to understand what I mean.
I just wonder how to implement such hash function.
PS: For math, i am an idiot. I can not imagine if there is an algorithm that can do this.
UPDATE 1:
The pseudocode has bug, so I updated the code(actually still has bug, never mind, I will explain).
My original requirement is to make a hash function that can return same value for different parameter, and the parameter value should contains some rule. It means, only the parameter value in same category would gets same hash code, others are not.
e.g.
The following expressions are clearly(you can treat '0' as placeholder):
hash("1.1") == hash("1.0") == hash("0.1")
hash("2.2") == hash("2.0") == hash("0.2")
and
hash("2.2") != hash("2.1") != hash("1.2")
I think this question can do such description:
There are two or more different values contains implied same attribute.
Only these values have such same attribute in the world.
The attribute can obtain through some way(maybe a function), hash() will call it inside.
hash() one of the values, you can retrive the attribute, then you can get the unique hashCode.
It's looks like hash collision, but we exactly know what they are. Also looks like many-to-one model.
How to design collision rules? The values could be any character or numeric. And how to implement the designs?
PPS: This is a question full of bugs, maybe the updated parts cannot explain the the problem either. Or maybe this is a false proposition. I want abstract my issue as a general model, but it makes my mind overflowed. If necessary I will post my actual issue that I am facing.
Any constant hash trivially satisfies your condition:
hash(v) = 42
A less constant answer than yuri kilocheck's would be to use the mod operator:
hash(v) = v % 10;
Then you'll have:
hash(1) = 1
hash(2) = 2
hash(3) = 3
...
hash(11) = 1
hash(12) = 2

Using Where() with a dynamic Func fails, but works with a hard coded Where() clause

See the two Linq (to SharePoint) code samples below.
The only differences are the highlighted sections of code. The first statement works as expected with a hard-coded where clause, but the 2nd set of code throws the error “Value does not fall in within the expected range” when I try to do a count on the items. What am I missing?
Works
relatedListItems = dc.GetList<GeneralPage>("Pages")
.Where(x => x.RelatedPracticesTitle.Any(y=>y=="Foo"))
if (relatedListItems.Count() == 0)
{…}
Fails - “Value does not fall within the expected range”
Func<GeneralPage, bool> f = x => x.RelatedPracticesTitle.Any(y => y == "Foo");
relatedListItems = dc.GetList<GeneralPage>("Pages")
.Where(f)
if (relatedListItems.Count() == 0)
{…}
If it's LINQ to Sharepoint, presumably that means it should be using expression trees, not delegates. Try:
Expression<Func<GeneralPage, bool>> f =
x => x.RelatedPracticesTitle.Any(y => y == "Foo");
relatedListItems = dc.GetList<GeneralPage>("Pages").Where(f);
By the way, it's generally a better idea to use Any() rather than Count() if you just want to find out if there are any results - that way it can return as soon as it's found the first one. (It also expresses what you're interested in more clearly, IMO.)
In the first case, you're using the Expression<Func<GeneralPage, bool>> overload and pass an expression which I assume LINQ to SharePoint will try to convert to CAML and execute.
In the second case, you're passing the plain Func<GeneralPage, bool> so LINQ to SharePoint can't figure out how to compose a query (it only sees the delegate, not the expression).

oracle if 0, like if null (nvl)

oracle has a nice built in function for doing if null, however I want to do if = 0; is there a simple way to do this?
nvl(instr(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5),' '),
length(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5))))
This is going as a parameter to a substr function.
If instr(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5),' ') is != 0 then I want that value, otherwise I want the value of length(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5))
is there an easy way to do this?
You can use NVL(NULLIF(A,'0'), B)
I found both answers hard to read because of the extra verbiage from the original post. Summarizing Conrad and Craig's answers:
To replicate nvl(A,B) but for 0 instead of null, you can do:
WHEN A != 0 THEN
A
ELSE
B
END
or Craig's more compact (but harder for others to read):
NVL(NULLIF(A,0),B)
I think that the following would also work:
DECODE(A,0,B,A)
I think you'll need to use CASE
e.g.
WHEN instr(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5),' ') != 0 THEN
length(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5))
ELSE
Some Default
END as foo
You could technically do this with less typing as:
nvl(
nullif(instr(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5),' '),0),
length(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5))
)
However, I would typically side with Conrad and advise you to use CASE so it is easier to tell what the intent of the code is for future maintenance.

Strange LINQ behavior - ToList returning empty set

I cant understand why resultSet2 is empty! Only the first assert passes!
List<Tree> resultSet1 = this.datacontext.Trees.Where(t=>t.RiskRating.Contains("bad")).ToList();
Assert.IsTrue(resultSet1.count() == 3);
List<Tree> resultSet2 = this.datacontext.Trees.ToList().Where(t=>t.RiskRating.Contains("bad")).ToList();
Assert.IsTrue(resultSet2.count() == 3);
Thanks!
Ashley
What does this.datacontext.Trees.ToList().Count() return?
Could it be something to do with your collation back at the database? The first example will convert the Contains("bad") method back to SQL, which might be case insensitive and return rows that contain "BAD" or "Bad". The second example wouldn't be case sensitive.
It'd be interesting to see what the values for RiskRating are back in the database, and what the SQL query getting executed looks like.

Resources