11/22/2024 8:55:38 AM
|
|
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!
Forum to discuss general external development topic (related or not to SalesLogix development). View the code of conduct for posting guidelines.
|
|
|
|
[sublogix] Advanced LINQ expressions?
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? |
|
|
|
Re: [sublogix] Advanced LINQ expressions?
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. |
|
|
| |
|
Re: [sublogix] Advanced LINQ expressions?
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.
|
|
|
|
Re: [sublogix] Advanced LINQ expressions?
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! |
|
|
| |
|
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!
|
|
|
|
|
|
|
|