Home > Back-end >  WebMethod returning multiple line (list<>) from SQL query in WCF with Entity Framework Linq
WebMethod returning multiple line (list<>) from SQL query in WCF with Entity Framework Linq

Time:06-22

Creating a WCF in VisualStudio 2017, I need to create a WebMethod that will return multiple rows from a SQL query. Here is the code that is not working...

code

    [WebMethod]
    public List<TABLE_NAME> GetAllLineInfoDetailes(string OrderNumberSM)
    {
        string @OrdNumLG  = OrderNumberSM;

        List<TABLE_NAME> OrdNo = new List<TABLE_NAME>();
        using (CONNECTION_NAME pubs = new CONNECTION_NAME())
        {
            var OrdNo_LINES = (from p in pubs.TABLE_NAME select p.OrderNumber == @OrdNumLG);

            foreach (TABLE_NAME OrderLine in OrdNo_LINES)
            {
                TABLE_NAME a = new TABLE_NAME();
                a.ItemNumber = OrderLine.ItemNumber;
                a.LineNumber = OrderLine.LineNumber;
                a.OrderNumber = OrderLine.OrderNumber;
                OrdNo.Add(a);
            }
        }
        return OrdNo;
    }

code

The foreach is giving error "Cannot convert type 'bool' to 'CONNECTION_NAME.TABLE_NAME'"

Any help with this, or a better way to return the full result set, would be appreciated.

CodePudding user response:

As the error says, it's a type conversion problem.
You need to

var OrdNo_LINES = (from p in pubs.TABLE_NAME select p.OrderNumber = @OrdNumLG);

replace

var OrdNo_LINES = (from p in pubs.TABLE_NAME select p.OrderNumber == @OrdNumLG);

CodePudding user response:

Found what will work...

[WebMethod]
    public List<TABLE_NAME> GetAllLineInfoDetailes(string OrderNumberSM)
    {
        string @OrdNumSM = OrderNumberSM;

        using (CONNECTION_NAME pubs = new CONNECTION_NAME())
        
        {
            var query = (from c in pubs.TABLE_NAME
                         where c.OrderNumber == @OrdNumSM
                         orderby c.LineNumber ascending
                         select c).ToList();

            return query;
        }
    }
  • Related