Home | Forums | Contact | Search | Syndication  
 
 [login] [create account]   Tuesday, April 16, 2024 
 
slxdeveloper.com Community Forums  
   
The Forums on slxdeveloper.com are now retired. The forum archive will remain available for the time being. Thank you for your participation on slxdeveloper.com!
 External Development Forums - General External Development
Forum to discuss general external development topic (related or not to SalesLogix development). View the code of conduct for posting guidelines.
Forums RSS Feed


 Back to Forum List | Back to General External Development | New ThreadView:  Search:  
 Author  Thread: [sublogix] Advanced LINQ expressions?
Andrei Kruglov
Posts: 11
 
[sublogix] Advanced LINQ expressions?Your last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 21 Jun 12 6:42 AM
fiogf49gjkf0d
I am started using sublogix and have some questions about it.

I have such simple query:

IList contactList = _repo.Select().Where("AccountId", Operator.Equals, slxAccountId).Execute();

It’s all good – but I have no auto-complete when I type “AccountId”, that’s why I write so:

IList contactList = _repo.Find(x => x.AccountId.Equals(slxAccountId));

Brilliant, but what about this kind of code:

IList contactList =
_repo.Select()
.Where("AccountId", Operator.Equals, slxAccountId)
.And("IsPrimary", Operator.Equals, "T")
.Execute();

I re-write this query like this:

IList contactList = _repo.Find(x => x.AccountId.Equals(slxAccountId) && x.Isprimary.Equals("T") );

Compile OK, but running this I’ve got error «Operation not yet supported. Right-side of expression must include constant or variable value.»

Is it really not supported in current (1.2.9) version in sublogix?
[Reply][Quote]
Ryan Farley
Posts: 2265
slxdeveloper.com Site Administrator
Top 10 forum poster: 2265 posts
 
Re: [sublogix] Advanced LINQ expressions?Your last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 12 Jul 12 12:57 AM
fiogf49gjkf0d

Yeah, the Linq expressions really need some work and are incomplete in the current version. It would work to chain them together, like this:



var contactList = _repo.Find<Contact>(x => x.AccountId == slxAccountId).Where(x => x.Isprimary == "T").ToList();


In the code above it is important to remember a few things. The first expression is used to retreive the records from the database, then the IList is refined down to a smaller set using the Linq Where. So, in the case of this example it isn't a huge deal. You'll get back all contacts for the current account and then the list is refined to contain only the one(s) where Isprimary == "T". In other cases this wouldn't work so well since you might bring back too much data to make it worth it. In these cases you'd want to use the _repo.Select even though you loose intellisense just for performance sake. I do have a version coming where you can select properties instead of including them as strings, but ideally I need to get the Linq expression tree parsing complete so you can write more complex expressions for Find. That will come, but I don't exactly have it on my radar for newar future yet.

[Reply][Quote]
Andrei Kruglov
Posts: 11
 
Re: [sublogix] Advanced LINQ expressions?Your last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 22 Aug 12 8:43 AM
fiogf49gjkf0d
OK, i'll use this workaround. But what about to put sublogix on git?
[Reply][Quote]
Andrei Kruglov
Posts: 11
 
Re: [sublogix] Advanced LINQ expressions?Your last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 19 Sep 12 5:45 AM
fiogf49gjkf0d

The simple way to get auto-complete feature.

Instead of


IList<Contact> contactList = Repository.Select<Contact>()
.Where("AccountId", Operator.Equals, slxAccountId)
.Execute();


Use


IList<Contact> contactList = Repository.Select<Contact>()
.Where(GetElementName<Contact>(x => x.AccountId), Operator.Equals, slxAccountId)
.Execute();



Also, you need add this code to your project:


public static string GetElementName<TItem>(this TItem obj, Expression<Func<TItem, object>> expression)
    where TItem : IEntity
{
    return GetElementName(expression);
}

public static string GetElementName<TItem>(Expression<Func<TItem, object>> expression)
    where TItem : IEntity
{
    var body = expression.Body as MemberExpression;
    if (body == null)
    {
        throw new ArgumentException("Expression is not a member access", "expression");
    }

    var entityElement = (EntityElementAttribute) body.Member
     .GetCustomAttributes(typeof (EntityElementAttribute), false)
     .Single();

    return entityElement.Name;
}



This works to me fine.


 


 

[Reply][Quote]
Ryan Farley
Posts: 2265
slxdeveloper.com Site Administrator
Top 10 forum poster: 2265 posts
 
Re: [sublogix] Advanced LINQ expressions?Your last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 20 Sep 12 1:03 AM
fiogf49gjkf0d

Andrei,


That is awesome. I've added this into a new helper class in Sublogix. You'll find this useful method as EntityHelper.GetElementName. Once you update to Sublogix version 1.3.1.0 (which has now been released to NuGet), you can now use this built-in without the need to add the method to your own code (you'll need to add a using directive for Sublogix.Helpers).


 


IList<Contact> contactList = Repository.Select<Contact>()
.Where(EntityHelper.GetElementName<Contact>(x => x.AccountId), Operator.Equals, slxAccountId)
.Execute();

 


Thanks again!

[Reply][Quote]
Andrei Kruglov
Posts: 11
 
Re: [sublogix] Advanced LINQ expressions?Your last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 20 Sep 12 5:44 AM
fiogf49gjkf0d

I've update sublogix upto 1.3.1.0 in my app - works well, thank you for including this feature into your project!

[Reply][Quote]
 Page 1 of 1 
  You can subscribe to receive a daily forum digest in your user profile. View the site code of conduct for posting guidelines.

   Forum RSS Feed - Subscribe to the forum RSS feed to keep on top of the latest forum activity!
 

 
 slxdeveloper.com is brought to you courtesy of Ryan Farley & Customer FX Corporation.
 This site, and all contents herein, are Copyright © 2024 Customer FX Corporation. The information and opinions expressed here are not endorsed by Sage Software.

code of conduct | Subscribe to the slxdeveloper.com Latest Article RSS feed
   
 
page cache (param): 4/16/2024 2:08:15 PM