new query syntax

Posts   
 
    
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 26-Mar-2006 06:42:58   

Attempting to use the new query syntax in llblgen for the first time. The docs (The predicate system) suggests I can do something like this:

Example 1 IPredicateExpression filter = (UserFields.userId == program.userid); // problem here IRelationPredictateBucket bucket = new RelationPredicateBucket(filter); adapter.FetchEntity(user, bucket);

The above generates an error:

Cannot implicitly convert type 'SD.LLBLGen.Pro.ORMSupportClasses.Predicate' to 'SD.LLBLGen.Pro.ORMSupportClasses.IPredicateExpression'. An explicit conversion exists (are you missing a cast?)

However if I do the following, everything works fine

Example 2 IPredicateExpression filter = new PredicateExpression(); filter.Add(UserFields.userid = program.userid); adapter.FetchEntity(user, bucket);

I know it's something obvious but what am I doing wrong using the first example. Please note that if I change IPredicateExpression to IPredicate in Example 1, everything works fine.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 26-Mar-2006 13:02:58   

erichar11 wrote:

Attempting to use the new query syntax in llblgen for the first time. The docs (The predicate system) suggests I can do something like this:

Example 1 IPredicateExpression filter = (UserFields.userId == program.userid); // problem here IRelationPredictateBucket bucket = new RelationPredicateBucket(filter); adapter.FetchEntity(user, bucket);

The above generates an error:

Cannot implicitly convert type 'SD.LLBLGen.Pro.ORMSupportClasses.Predicate' to 'SD.LLBLGen.Pro.ORMSupportClasses.IPredicateExpression'. An explicit conversion exists (are you missing a cast?)

Correct. (UserFields.userId == program.userid) returns a FieldCompareValuePredicate, which derives from Predicate. You can't equal it to a predicateexpression. You should do: IRelationPredictateBucket bucket = new RelationPredicateBucket((UserFields.userId == program.userid)); adapter.FetchEntity(user, bucket);

Frans Bouma | Lead developer LLBLGen Pro
Gabbo
User
Posts: 56
Joined: 12-Jun-2006
# Posted on: 28-Aug-2006 18:02:13   

Hi Otis,

The latest v2 documention still suggests you can do what the poster had originally shown, which returns an error. Is this an error in the documentation or some very subtle difference between the example in the documentation (The predicate system, Constructing Predicate Expressions) and the example the original poster had used?

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 28-Aug-2006 18:19:22   

Hi,

I can't find in the documentation what you are referring to. Can you give us a pointer?

But now maybe you're referring to lines like:


IPredicateExpression B = ((Table1Fields.Foo == "One") & (Table1Fields.Bar == "Two")) // A
    | !(Table2Fields.Bar2 == "Three");

This is valid because the & and | operator overloads will implicitely build the predicate expression

Gabbo
User
Posts: 56
Joined: 12-Jun-2006
# Posted on: 28-Aug-2006 18:34:27   

Hi Jesse,

Actually, that is the example I'm referring to. I think it would be useful to at least two newbies (myself and the original poster) if an example using a single field was either shown in this part of the documentation or at least referred to in some way here (perhaps saying something like -- if building a single field comparison search, see the section FieldCompareExpressionPredicate further below). The documentation certainly states that the overloaded operators "&" and "|" will create predicate expressions, but to someone just getting up to speed on how to create filters, what this means when trying to create a filter using a single field might not be clear.

Thanks for responding so quickly!

wojo
User
Posts: 69
Joined: 10-Mar-2004
# Posted on: 28-Aug-2006 19:44:22   

Could an implicit operator conversion method be added to predicates to convert them into predicate expressions? Something like this:

In PredicateExpression's implementation:

public static implicit operator IPredicateExpression(IPredicate predicate)
{
  IPredicateExpression predicateExpression = new PredicateExpression(predicate);
  return predicateExpression;
}

In RelationPredicateBucket's implementation:

public static implicit operator IRelationPredicateBucket(IPredicateExpression predicateExpression)
{
  IRelationPredicateBucket bucket = new RelationPredicateBucket(predicateExpression);
  return bucket;
}

Then, for example, instead of having to do this:

adapter.FetchEntity(user, bucket);

you could do this:

adapter.FetchEntity(user, UserFields.Username = "foo");

I didn't try this out, but I think it could work? It'd be nice to not have to create buckets when I don't have relations and not to have to create predicate expressions manually when I have one predicate (which is an expression, just with one predicate).

-- Robert

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 29-Aug-2006 07:29:30   

Good suggestion, but I guess you ment FetchNewEntity() or FetchEntityCollection() rather than FetchEntity()

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 29-Aug-2006 10:32:03   

I'll try, but I have my doubts it will work due to the interfaces specified.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 29-Aug-2006 16:57:44   

It won't work, you can't convert to / from an interface: error CS0552: 'SD.LLBLGen.Pro.ORMSupportClasses.PredicateExpression.implicit operator SD.LLBLGen.Pro.ORMSupportClasses.PredicateExpression(SD.LLBLGen.Pro.ORMSupportClasses.IPredicate)': user-defined conversion to/from interface

I already had to change IPredicateExpression in the first method to PredicateExpression to get rid of the error that you have to convert to/from the containing type.

If I change IPredicate to Predicate, it also doesn't compile: error CS0553: 'SD.LLBLGen.Pro.ORMSupportClasses.PredicateExpression.implicit operator SD.LLBLGen.Pro.ORMSupportClasses.PredicateExpression(SD.LLBLGen.Pro.ORMSupportClasses.Predicate)': user-defined conversion to/from base class

So that's the end of the line for this one. It was a nice suggestion though simple_smile

Frans Bouma | Lead developer LLBLGen Pro
wojo
User
Posts: 69
Joined: 10-Mar-2004
# Posted on: 29-Aug-2006 18:04:07   

Walla: correct, I just copied what was above without thinking simple_smile

Otis: I tried this earlier today and yeah, it won't work for the Predicate -> PredicateExpression conversion because PredicateExpression derives from Predicate and C# doesn't allow you to define an operator (implicit or explicit) that converts from a base class or interface.

However, you can do this for a Predicate -> RelationPredicateBucket like so:

        public static implicit operator RelationPredicateBucket(Predicate predicate)
        {
            RelationPredicateBucket bucket = new RelationPredicateBucket(predicate);
            return bucket;
        }

            // works
            RelationPredicateBucket bucket = CarFields.Initials == "CSXT";
            a.FetchEntityCollection(c, bucket);

            // works
            a.FetchEntityCollection(c, (RelationPredicateBucket) (CarFields.Initials == "CSXT"));

            // doesn't work, can't implicitly cast
            a.FetchEntityCollection(c, CarFields.Initials == "CSXT");

Now, is that the correct thing to do anyway? Pulling back a little, I'm wondering if it is abuse of the language features. It doesn't seem so for the Predicate -> PredicateExpression conversion, to me, but it does seem a little dirty for the Predicate -> RelationPrediacteBucket.

Anyway, it's very easy to just do this anyway:

a.FetchEntityCollection(c, new RelationPredicateBucket(CarFields.Initials == "CSXT"));

Waiting for LINQ ... wink

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 30-Aug-2006 10:26:17   

I indeed find it a bit abusive, as the relation between a predicate like a FieldCompareValuePredicate and a relationpredicatebucket is IMHO too loose to make it logical to have the conversion (compared to int<->byte for example).

Yeah linq... simple_smile . I've requested from MS the information needed to write a Linq compatible conversion layer for llblgen pro. Let's see what they come up with simple_smile

Frans Bouma | Lead developer LLBLGen Pro