Search code examples
c#sql-serverlinqselectlinqdatasource

LINQ: LinqDataSource How to do column Select in codebehind?


<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    Select="new(Key as ProductCategory, 
            Average(Price) as AvePrice)"
    ID="LinqDataSource1" 
    runat="server">
</asp:LinqDataSource>

Somehow my Select in .aspx file (as seen above) is not working: all columns are returned in the query result. So I will try to do that in code behind.

How do I perform the selection of the 2 fields in my LinqDataSource1_Selecting ()? Thanks.


Solution

  • for example:

    //notice: condition is sample

    int ave ;
    
    Queryable<Object> IQ = ContextTypeName.TableName.Where(x=>x.Price <= ave);
    

    OR

    var Query = FROM objectNameSeleted IN ContextTypeName.TableName
                WHERE (your condition) SELECT objectNameSeleted