Subsonic 3: Update<T> problem - subsonic

I am using SS 3.0.0.3 ActiveRecord. This query:
new Update<BillDetail>(provider)
.Set(bd => bd.DivisionDetails == divisionDetails)
.Where(bd => bd.BillNumber == documentId && bd.IsInvoice == true);
produces this SQL:
UPDATE `BillDetails`
SET `DivisionDetails`=#up_DivisionDetails
WHERE `BillNumber` = #0
Where did the IsInvoice part go? Am I doing something wrong here?

Had this problem in previous releases but it was fixed - you sure you're using 3.0.0.3?

Finally fixed in 3.0.0.4.

Related

Gosu (Guidewire)

If we need to consider two states of claim (e.g - Draft & Closed state) for temporary claim, then how can we use these state using Query?
I tried with -
var claims = Query.startswith("ClaimNumber", "TMP", false)
.compareIn(Claim#State, {ClaimState.TC_DRAFT, ClaimState.TC_CLOSED}.toArray())
.select()
The above line throws null pointer error . Could anyone help on this ?
I believe the Claim Entity has enough data to be queried.
Also i see the "make" statement is missing in the code given by you.
Try the below query in your Gosu Scratchpad,
uses gw.api.database.Query
var claims = Query.make(Claim)
.startsWith("ClaimNumber","TMP",false)
.compareIn(Claim#State, {ClaimState.TC_DRAFT, ClaimState.TC_CLOSED} as ClaimState[])
.select()
for(claim in claims){
print(claim.ClaimNumber)
}
If you still face any issues, please provide the exception that you get.
Please mark the answer as correct if my info solved your issue.
Try with or block,
uses gw.api.database.Query
var queryCliamState= Query.make(entity.Claim)
.compare("ClaimNumber", Equals, "12345")
.or(\orCondition -> {
orCondition.compare("State" , Equals,typekey.ClaimState.TC_DRAFT)
orCondition.compare("State" , Equals,typekey.ClaimState.TC_CLOSED)
}) .select()
-When you are comparing make sure the state is equal to the database field.
Looks like you are missing the make sentence:
var claims = Query.make(Claim).startsWith("ClaimNumber", "TMP", false)
.compareIn(Claim#State, {ClaimState.TC_DRAFT, ClaimState.TC_CLOSED}.toArray())
.select()
It should work after that.

How to set timeout for NHibernate LINQ statement

I am using Fluent NHibernate for my ORM. In doing so I am trying to use the NHibernate LINQ syntax to fetch a set of data with the power of LINQ. The code I have works and executes correctly with the exception being that a timeout is thrown if it takes longer than roughly 30 seconds to run. The question I have is how do I extend the default 30 second timeout for LINQ statements via NHibernate?
I have already seen the posts here, here, and here but the first two refer to setting the DataContext's Timeout property, which does not apply here, and the third refers to setting the timeout in XML, which also does not apply because I am using Fluent NHibernate to generate the XML on the fly. Not only that but the post is 2 years old and Fluent NHibernate has changed since.
With the ICriteria objects and even HQL I can specify the timeout, however that is not the goal here. I would like to know how to set that same timeout and use LINQ.
Example code:
using (var session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
var query = (from mem in session.Query<Member>()
select mem);
query = query.Where({where statement});
int start = (currentPage - 1) * max);
if (start > 0)
query = query.Skip(start).Take(max);
else
query = query.Take(max);
var list = query.ToList();
transaction.Commit();
return list;
}
This code (where statement does not matter) works for all purposes except where a timeout occurs.
Any help is appreciated. Thanks in advance!
I ended up setting the command timeout for the Configuration for Fluent NHibernate. The downside to this is that it sets the timeout for ALL of my data access calls and not just the one.
Example code:
.ExposeConfiguration(c => c.SetProperty("command_timeout", (TimeSpan.FromMinutes(10).TotalSeconds).ToString()))
I found this suggestion from this website.
Nhibernate has extended the IQueryable and added a few methods https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Linq/LinqExtensionMethods.cs
var query = (from c in Session.Query<Puppy>()).Timeout(12);
or
var query = (from c in Session.Query<Puppy>());
query.Timeout(456);
I've just spent fair amount of time fighting with this and hopefully this will save someone else some time.
You should use the .Timeout(120) method call at the very last moment to make sure it is used. TBH I'm not 100% sure on why this is but here are some examples:
WILL WORK
query = query.Where(x => x.Id = 123);
var result = query.Timeout(120).ToList();
DOESN'T WORK
query.Timeout(120);
query = query.Where(x => x.Id = 123);
var result = query.ToList();
If done like the second (DOESN'T WORK) example, it seems to fall back to the default System.Transaction.TransactionManager.DefaultTimeout.
Just in case anyone is still looking for this and finds this old thread too...
Query.Timeout is deprecated.
You should use WithOptions instead:
.WithOptions(o => o.SetTimeout(databaseTimeoutInSeconds))

Get an object from ObservableColelction with condition

I have an ObservableColection totalsCol and want to retrieve an object whose id matches the specified id. I coded as :
IEnumerable<Totals> ie = totalsCol.Where(a => a.IdCTS == ct1.TOR_Id);
if (ie.Count() > 1)
{
// Update the TotalCts of Totals object
ie.ElementAt(0).TotalCTS = ct1.TotalCts;
CalculateTotalsPercent();
}
I get ie.count as null. Whereas it has 3 records. And on debugging, I can see that under Source of base.
Where am I wrong here ? I beleive the way am updating the the value with ie.ElementAt will reflect changes in totalsCol observableCollection.
Kindly help me out.
I implemented :
totalsCol.First(a => a.IdCTS == ct1.TOR_Id);
and solved the issue.

Complex queries in linq to nhibernate

We are using accountability pattern for organizational structure. I using linq to nhibernate to find some departments and position but I have two problem.
var query =
Repository<Party>.Find(p => p.IsInternal == true)
.Where(p => p.Parents.Any(c => c.Parent.PartyId == id))
.Where(p =>
(
p.PartyType.PartyTypeId == (int)PartyTypeDbId.Department &&
_secretariat.Departments.Select(c => c.PartyId).Contains(p.PartyId)
)
||
(
p.PartyType.PartyTypeId == (int)PartyTypeDbId.Position &&
p.Children.Any(c =>
c.AccountabilityType.AccountabilityTypeId == (int)AccountabilityTypeDbId.TenurePersonOfPosition &&
((Person)c.Child).UserName != null)
)
);
First : I got 'Unhandled Expression Type: 1003' for this part of query : '_secretariat.Departments.Select(c => c.PartyId).Contains(p.PartyId)'
and I got Property not found 'UserName'
We have many complex queries i think we need to use stored procedure.
Sorry for bad Inglish!
One nice thing that you can do with LINQ is break your queries into multiple parts. Since you are building an expression tree that won't get executed until the results are enumerated, you don't have to do it all in one line (like SQL).
You can even make some reusable "filters" that you can apply to IQueryable. These filter functions accept an IQueryable as an argument, and return one as a result. You can build these as extension methods if you like (I like to).
As for your immediate problem, you may want to try a join on _secretariat instead of attempting a subquery. I've seen them work in scenarios where subqueries don't.
In addition to Lance's comments you might want to look at a compiled Linq query and breaking up some of the responsibilties to follow SOLID principles.
I've just also found out that there are issues with the Contains when containing Any linq methods. However, Any seems to work well within Any, hence:
_secretariat.Departments.Select(c => c.PartyId).Any(x => x == p.PartyId)

Subsonic Delete multiple records

I have a table which has a two fields, userid and degreeid. A user can have multiple degreeds in this table. When a user select their degree I want to delete any degree id already in the database but un-selected by user. How should do it? I try to use something like this with no luck.
repo.DeleteMany(x=>x.MeetingAttendeeUserID==userID && x.EducationDegreeID userDegreeList)
Thanks,
Lewis
Could you do something like this?
repo.DeleteMany(x =>
x.MeetingAttendeeUserId == userID &&
x.EducationDegreeID != selectedDegreeId
);
Edit: Or this.
repo.DeleteMany(x =>
x.MeetingAttendeeUserId == userID &&
userDegreeList.Contains(x.EducationDegreeID))
);
I have tried
SubSonicRepository<ReportDatum> repo = new SubSonicRepository<ReportDatum>(db);
repo.DeleteMany(x => x.ReportID == Convert.ToInt32(reportID));
and it does nothing, not even an error.
Addendum for someone that comes across this:
don't convert in the delete call, convert your variables beforehand.
int deleteKey = Convert.ToInt32(reportID)
SubSonicRepository<ReportDatum> repo = new SubSonicRepository<ReportDatum>(db);
repo.DeleteMany(x => x.ReportID == deleteKey);
and it fired fine.
I would recommend if you dont know Lamda expression. Create your own stored procedure and call use it.
This will not restrict you using lamda expression if you don't have time to learn it.
Ultimately you wanted to done your job.

Resources