Solved

call function in a linq query

Posted on 2014-02-06
5
2,086 Views
Last Modified: 2016-02-10
hi there

i need to call a func inside the linq query my code is this ;
  public List<exlids> GetLeadByID(long lidid)
        {
            using (EF.lidsDBEntities ef = new EF.lidsDBEntities())
            {
                //var lid = ef.Lids.Where(l => l.Lid_Id == lidid).SingleOrDefault();
                //return lid;

                var lid = from l in ef.Lids
                          join c in ef.Customers
                          on l.CID equals c.CID
                          where l.Lid_Id == lidid
                          select new exlids
                          {
                              lidExs = l,
                              cust_name = c.Cust_Name,
                              cust_phone = c.Cust_Phone,
                              lead_his = GetLidHistory(c.CID) // here its faild   
                      };
                return lid.ToList();

            }
        }
        public string GetLidHistory(long cid)
        {
            string his = string.Empty;
            using (EF.lidsDBEntities ef = new EF.lidsDBEntities())
            {
                var history = ef.GetLidHistory(cid);
                foreach (var item in history)
                {
                    his += item.Lid_Id.ToString() + ' ' + item.lid_open_date.ToString() + "\n\n" + item.lid_Desc +"\n\n";
                }
                return his;
            }
        }
    }

Open in new window


i am getting this Exsption :
: LINQ to Entities does not recognize the method 'System.String GetLidHistory(Int64)' method, and this method cannot be translated into a store expression.

how can i fix that ?
thanks ...
0
Comment
Question by:Tech_Men
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39840113
Hi Tech_Men;

What you want to do is NOT possible. The reason is that the query gets translated into TSQL and in TSQL there is no GetLidHistory function to translate to. You need to remember that the TSQL get executed at the server and has no access to your local code. The only way to do this is to bring the data down to the local machine then run a query on the results of the Linq to EF or Linq to SQL locally where you can call local functions.
0
 

Author Closing Comment

by:Tech_Men
ID: 39840129
thanks...
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39840138
Not a problem Tech_Men, glad I was able to help.
0
 

Author Comment

by:Tech_Men
ID: 39840165
i just change it like that and its work fine :

 using (EF.lidsDBEntities ef = new EF.lidsDBEntities())
            {
                var cid = ef.Lids.Where(li => li.Lid_Id == lidid).Select(s => s.CID).ToList().SingleOrDefault();
                string his = GetLidHistory(cid);
                var lid = from l in ef.Lids
                          join c in ef.Customers
                          on l.CID equals c.CID
                          where l.Lid_Id == lidid
                          select new exlids
                          {
                              lidExs = l,
                              cust_name = c.Cust_Name,
                              cust_phone = c.Cust_Phone,
                              lead_his = his
                          };
                return lid.ToList();

            }
        }

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39840220
Very good.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question