I Cannot see if this is a bug or my ignorance as the original logic and work around solution logic are so similar
I am building up a complex series of queries. This includes a drop down box for users to choose if they want to check "starting with", "Contains" or ends with (values 0,1 & 2)
My first draft class to do the query contains the following code. I use a lot of generalisation and common libraries. I use objects that can have variables created and set within the object through a function called SetCriteria. I can then get the value of the dynamic variable through GetCriteria (which returns null if it doesn't exist). This is a cut down, but salient, part of the code I am testing
object objhowSearchCompany = null;
object objCompany = null;
//param1 = private var name, param 2 is ref to local object to hold a copy
GetCriteria("howSearchCompany", out objhowSearchCompany);
GetCriteria("Company", out objCompany);
string company = (string)objCompany;
int howSearchCompany = (int)objhowSearchCompany;
var Query = (from Name in entity.TblMyName
where
(
//when a user doesn't fill the company field on the e-form the search object
//will not have a var company (so will be null) or
//blanked out (so will have length 0)
//presuming boolean conversion is good enough not to do the
//comparison if the search criteria is false
( company.Length == 0) ||
( company.Length == null) ||
( howSearchCompany == 0 && Name.TblName.Company.ToUpper().StartsWith(company.ToUpper()) ) ||
( howSearchCompany == 1 && Name.TblName.Company.ToUpper().Contains(company.ToUpper()) ) ||
( howSearchCompany == 2 && Name.TblName.Company.ToUpper().EndsWith(company.ToUpper()) )
)
select new
{
Name = Name.TblName.Surname.TrimEnd() + " " + Name.TblName.TblTitle.Description.TrimEnd() + " " + Name.TblName.Forename.TrimEnd(),
OIDName = Name.TblName.OIDRef,
OIDAddress = Name.TblAddresses.OIDAddress,
Company = Name.TblName.Company
}
);
If I compile without the where line including the EndsWith then the query works perfectly and provides the right results.
If I include the EndsWith then I get an inner exception error.
Now I have a work around that seems to stop giving me the innerexception error
object objhowSearchCompany = null;
object objCompany = null;
GetCriteria("howSearchCompany", out objhowSearchCompany);
GetCriteria("Company", out objCompany);
string company = "";
if (objCompany != null)
company = ((string)objCompany).ToUpper();
int howSearchCompany = (int)objhowSearchCompany;
var Query = (from Name in entity.TblMyName
where
(
( company.Length == 0) ||
( company.Length == null) ||
( howSearchCompany == 0 && Name.TblName.Company.ToUpper().StartsWith(company) ) ||
( howSearchCompany == 1 && Name.TblName.Company.ToUpper().Contains(company) ) ||
( howSearchCompany == 2 && Name.TblName.Company.ToUpper().EndsWith(company) )
)
select new
{
Name = Name.TblName.Surname.TrimEnd() + " " + Name.TblName.TblTitle.Description.TrimEnd() + " " + Name.TblName.Forename.TrimEnd(),
OIDName = Name.TblName.OIDRef,
OIDAddress = Name.TblAddresses.OIDAddress,
Company = Name.TblName.Company
}
);
Now the second version does NOT give me a run time inner exception error, which looking at the code baffles me because the GetCriteria for company in both sets of code returns the same value. An object containing "dan".
I do not like not understanding why one version works and the other not.
Can someone tell me why the first version fails?
Kerry
endswith causing confusion in linq. Have work around but....
-
- Posts: 52
- Joined: Tue 05 Jan 2010 12:26
1. Please specify the error message and your call stack.
2. Which version (x.xx.xx) of dotConnect for PostfreSQL are you using? You can find it via the Tools | PostgreSQL | About menu of Visual Studio.
3. Please send us a small test project with the DDL/DML script to reproduce the error.
2. Which version (x.xx.xx) of dotConnect for PostfreSQL are you using? You can find it via the Tools | PostgreSQL | About menu of Visual Studio.
3. Please send us a small test project with the DDL/DML script to reproduce the error.