Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

call function in a linq query

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

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 64

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 64

Expert Comment

by:Fernando Soto
ID: 39840220
Very good.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

618 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