Page 1 of 1

How to use LINQ Query .Any function

Posted: Sun 12 Feb 2017 02:10
by borisb
The LINQ syntax guide says the Any function
Any clause returns True if the input sequence contains any elements, otherwise False
and shows the following example:

Code: Select all

Linq.From(Emp)
    .Select([Emp['EmpNo'], Emp['EName']])
    .Any(Emp['Sal'] >= 1000)
However, in the following code:

Code: Select all

function TdmSecurityService.IsValidLogin(AUsername, APassword: string): boolean;
var
  Context: TDQCLabManagerDataContext;
  Users: IUserEntityExpression;

begin
  Context := dmDatabaseService.CreateContext;
  Users := Context.UserEntity;

  Result := Linq.From(Users)
    .Select
    .Any(
      (Users['Username'] = AUserName)
      AND
      (Users[Context.Model.UserEntity.Password.Name] = APassword ));
end;
Gives a compile error:
[dcc64 Error] D_SecurityService.pas(48): E2010 Incompatible types: 'Boolean' and 'ILinqQueryable'
What's wrong?

Re: How to use LINQ Query .Any function

Posted: Fri 17 Feb 2017 09:58
by AlexP
Hello,

Please provide the model file to support*devart*com, in order for us to be able to reproduce the problem.

Re: How to use LINQ Query .Any function

Posted: Fri 17 Feb 2017 21:00
by borisb
It has nothing to do with the model :-)

The problem is that .Any (like .Count) returns an ILinqQueryable instead of a boolean (or int in the case of .Count).

Can you send me a sample project instead that demonstrates that calling .Any returns a boolean...?

Re: How to use LINQ Query .Any function

Posted: Mon 24 Apr 2017 10:32
by AlexP
The provided code does not cause the specified error, please, send us your model and Delphi code to support*devart*com

Re: How to use LINQ Query .Any function

Posted: Thu 25 May 2017 14:17
by jwrz
Hi, I have the same problem. Question is how to compare this query:

Code: Select all

Linq.From(Emp)
    .Select([Emp['EmpNo'], Emp['EName']])
    .Any(Emp['Sal'] >= 1000)
to boolean value?

Re: How to use LINQ Query .Any function

Posted: Thu 25 May 2017 15:12
by borisb
I cancelled my EntityDac subscription because I failed to get an answer, and because these LINQ operators don't return the documented values (instead they return entity sets it seems). Switched to TMS Aurelius instead, which works as advertised.

Re: How to use LINQ Query .Any function

Posted: Fri 26 May 2017 12:06
by MaximG
In EntityDAC an execution result of any Linq.XX method will be a value of the ILinqQueryable type which contains a link to the compiled query (but not its result).
To get a result, the query should be materialized, for example using the TEntityContext.GetEntities method. As a result, the value of the IEnumerable type will be retrieved
which provides access to the collection which is the result of the query execution. You can find the sample of obtaining a result of the .Any method execution in the demo
distributed with EntityDAC (in the "Working with LINQ queries -> Quantifiers -> Any - Condition" section).

In case of using the .Any method, the collection will contain a single element of the TEntity type which have a single attribute of the Boolean type.
To retrieve the attribute value, you can use the below code (the code sample is provided for the TFrameLINQQuant.AnyCondition method from EntityDAC demo):

Code: Select all

ShowMessage(BooltoStr((Result as IEntityEnumerable)[0].Attributes[0].AsBoolean, True));

Re: How to use LINQ Query .Any function

Posted: Fri 26 May 2017 12:11
by borisb
Thanks, Maxim. Had you posted this 3 months ago when I originally inquired I wouldn't have cancelled my subscription :-(

Can the online documentation/help be updated with this technique?

Re: How to use LINQ Query .Any function

Posted: Mon 29 May 2017 08:00
by MaximG
Thank you for the patience. As a result of a technical malfunction in our forum work, we did not receive the corresponding notification in time. Usually our users get answers to all their questions within 24 hours. We hope that in the future such problems will no longer occur. We regret the incident and can offer you a free extension of your subscription. For this, provide your license number using the e-support form (https://www.devart.com  - the "Support" \ "Request Support" menu) . In turn, we will make the necessary changes to the documentation on EntityDAC