How to use LINQ Query .Any function

Discussion of open issues, suggestions and bugs regarding EntityDAC
Post Reply
borisb
Posts: 6
Joined: Mon 12 Sep 2016 12:22

How to use LINQ Query .Any function

Post by borisb » Sun 12 Feb 2017 02:10

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?

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: How to use LINQ Query .Any function

Post by AlexP » Fri 17 Feb 2017 09:58

Hello,

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

borisb
Posts: 6
Joined: Mon 12 Sep 2016 12:22

Re: How to use LINQ Query .Any function

Post by borisb » Fri 17 Feb 2017 21:00

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...?

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: How to use LINQ Query .Any function

Post by AlexP » Mon 24 Apr 2017 10:32

The provided code does not cause the specified error, please, send us your model and Delphi code to support*devart*com

jwrz
Posts: 12
Joined: Mon 22 Aug 2016 05:30

Re: How to use LINQ Query .Any function

Post by jwrz » Thu 25 May 2017 14:17

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?

borisb
Posts: 6
Joined: Mon 12 Sep 2016 12:22

Re: How to use LINQ Query .Any function

Post by borisb » Thu 25 May 2017 15:12

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.

MaximG
Devart Team
Posts: 974
Joined: Mon 06 Jul 2015 11:34

Re: How to use LINQ Query .Any function

Post by MaximG » Fri 26 May 2017 12:06

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));

borisb
Posts: 6
Joined: Mon 12 Sep 2016 12:22

Re: How to use LINQ Query .Any function

Post by borisb » Fri 26 May 2017 12:11

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?

MaximG
Devart Team
Posts: 974
Joined: Mon 06 Jul 2015 11:34

Re: How to use LINQ Query .Any function

Post by MaximG » Mon 29 May 2017 08:00

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

Post Reply