Solved

call function in a linq query

Posted on 2014-02-06
5
1,927 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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