?
Solved

call function in a linq query

Posted on 2014-02-06
5
Medium Priority
?
2,195 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

771 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