You need a field compare expression predicate, as described in Filtering and sorting in the manual. Please read that section of the manual as it's a very important one.
I'm not sure if you're using 1.0.2005.1 or v2.0, but either way, you can create predicates more easily using the operator overloading syntaxis. Please read the posting guidelines: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=7722 so we get as much information as possible to answer your question better.
You should use the _entityname_Fields classes to create field objects, so you can create an a.Field = b.Field predicate by simplydoing:
IPredicate p = (AFields.Field == bFields.Field);
Your code then is much cleaner
Your query is also inefficient, if I understand it correctly. What your query does is mainly fetching the entity from table1 which has the highest value for Field1.
This is the same as:
SELECT TOP 1 *
FROM Table1
ORDER BY Field1 DESC
This is much more efficient.
This then leads to the code:
EntityCollection currencyRate = new EntityCollection(new CurrencyRateEntityFactory());
SortExpression sorter = new SortExpression();
sorter.Add(CurrencyRateFields.ModifyDate | SortOperator.Descending);
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
adapter.FetchEntityCollection(currencyRate, null, 1, sorter);
}
As you can see, the sortclause is created with the '|' operator and the CurrencyRateFields class, which is available to you in the HelperClasses namespace of the generated code.
If you really want to do it using your query, you should use the following code instead:
EntityCollection currencyRate = new EntityCollection(new CurrencyRateEntityFactory());
RelationPredicateBucket filter = new RelationPredicateBucket();
filter.Add(new FieldCompareSetPredicate(
CurrencyRateFields.ModifyDate, null,
CurrencyRateFields.ModifyDate.SetAggregateFunction(AggregateFunction.Max), null,
SetOperator.Equal,
(CurrencyRateFields.ID==CurrencyRateFIelds.ID.SetObjectAlias("b"),
null,
"b",
1,
null,
false)));
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
adapter.FetchEntityCollection(currencyRate, filter);
}
As you can see, I use again the CurrencyRateFields class and use methods available on entityfield2 objects to set the aggregatefunction on a field and the alias. I alias only the subquery's field, not the normal query's field.