Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a linq to sql application. Inside it I have a method getGlassLens() that selects some columns from db and for each returned row builds a new instance of a class Glass_Lens.

I need to call another functions for each selected row, this fuction getGlassLensCats() returns a value which is used in the constructor of the class "Glass_Lens". This function returns a value that is not directly contained in the database, so it performs some operations to specify the value to be returned.

my question is how to call this function for each selected row? I tried inside the query, but clearly, it has no translation into sql

C#
public List <Glass_Lens> getGlassLens (long cat_id)
   {
       DataClasses1DataContext dc = new DataClasses1DataContext();
       var q =
       from a in dc.GetTable<glass_len>()
       //orderby a.id descending
       select a;

       if (cat_id != 0)
           q = q.Where(a => a.cat_id.Equals(cat_id));

       var result =
       q.Select(a => new Glass_Lens(a.id, a.piece_cost, a.piece_price, a.number,    a.insert_date, a.cat_id, getGlassLensCats(a.id).First().ToString())/* Here where I want to call my function but actually this makes no sense*/)
        .ToList();
       return result;
   }
Posted
Updated 8-Jul-14 21:39pm
v2
Comments
PrakashCs.net 9-Jul-14 7:34am    
Why do you want to call function in select query ?

instead of that use table join Glass_Lens and Glass_Lense_cat

from t1 in Glass_Lens
join t2 in Glass_Lense_cat on t1.catid equals t2.catid
select new { t1.col1, t2.col2}
johannesnestler 9-Jul-14 10:58am    
just query your database with your linqtosql query. then use linq to objects (or manual looping ...) to aggregate your getGlassLensCats results for each databaserow. After that use both informations to create your objects. I think your confusion Comes form using different Linq Providers. First your Expression will translate to SQL (you said you understand that this won't work for your method). So just make it two "steps" 1. query data, 2. calculate Information for each result (object) from the first query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900