Solved

call function in a linq query

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

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 62

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 62

Expert Comment

by:Fernando Soto
ID: 39840220
Very good.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now