Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I return list? 2

Status
Not open for further replies.

Xsi

Programmer
May 29, 2015
121
0
0
SE
Hi guys I have made following program,

I don't get any result in the console.
also how do I return the list result in console?

Code:
namespace MyProgram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Company test = new Company();
            test.CompanyN();
            

        }
    }
    public class Company
    {

        public IEnumerable<Company> CompanyN(int Cname)
        {
            myDb tdc = new myDb();
            var Result = tdc.ExecuteQuery<Company>(@"SELECT TOP (10) [t0].[Id], [t0].[IdAnonymous], [t0].[Name], [t0].[Address1], [t0].[Address2], [t0].[PostalCode], [t0].[City], [t0].[Country], [t0].[Phone], [t0].[Fax], [t0].[Email], [t0].[Web], [t0].[VAT], [t0].[Agreement], [t0].[AgreementEndDate], [t0].[TradosTM], [t0].[PathRefMaterial], [t0].[PaymentTerms], [t0].[Owner], [t0].[CreationDate], [t0].[PriceListName], [t0].[Currency], [t0].[IsActive], [t0].[LogoBlob]FROM [CompanyMain] AS [t0]");

            foreach (var c in Result)
            {
                Console.WriteLine(c);
            }
            return Result.ToList();
        }
    
    }
}


Could someone help me?

Thank you in advance


 
Two things:

First, I'm pretty sure as is you would be getting errors by not passing an [tt]int[/tt] when calling [tt]test.CompanyN()[/tt].

Second, you are returning an [tt]IEnumerable[/tt]. [tt]IEnumerable[/tt] is a promise that it is a list, it starts executing upon first access to it.

I see a couple of solutions:

Code:
Company test = new Company();
test.CompanyN().ToList();

or change the method

Code:
public List<Company> CompanyN(int CName)
 
Hi #Borvik
this is my final code:
but I get the error:

No overload for method 'CompanyN' takes 0 arguments

Code:
namespace MyProgram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


            Company test = new Company();
            test.CompanyN().ToList();

        }

    }
    public class Company
    {
        public IEnumerable<Company> CompanyN(int Cname)
        {
            myDb tdc = new myDb();
            var result = tdc.ExecuteQuery<Company>(@"SELECT Id FROM  CompanyMain");
            foreach (var c in result)
            {
                Console.WriteLine(c);
            }
            return result.ToList();
        }
    }
}
 
This will still cause an error. Your CompanyN method requires an integer parameter.
In your Form_Load event you are not specifying an integer when you call it. Since you are not using it in the method i assume you dont need it so remove it from the parameters.



Do you want some custom SIM scripts developed. Contact me via my website
 
Exactly the error I would have expected (see the "First" item in my other post). [tt]CathalMF[/tt] is correct, it doesn't appear you are using the parameter - I would remove it. Either that or call the method like this:

Code:
test.CompanyN(0).ToList();
 
Hi again

Thank you.

CathalMF#

I wonder why the loop doesn't runs?

I tested diffferent index values
test.CompanyN(0).ToList();
test.CompanyN(1).ToList(); etc..


2015-11-25_09_06_17-HAL_Statistics_2015-11-23_Debugging_-_Microsoft_Visual_Studio_Administrator_nwh1dv.png



Thank you in advance.
 
Different index values aren't really going to help - you aren't using the value anywhere.

By "the loop doesn't run" do you mean to say that [tt]Console.WriteLine[/tt] is never hit?

If so, have you tried checking the value of [tt]result[/tt] in the debugger after [tt]ExecuteQuery[/tt] has finished running (put a breakpoint on the [tt]foreach[/tt], and use the debugger watch windows, or mouse-hover, on [tt]result[/tt] to see what it contains).
 
Hi Borvik,


2015-11-26_09_00_05-HAL_Statistics_2015-11-23_Debugging_-_Microsoft_Visual_Studio_Administrator_ojuoma.png


Thank you in advance
 
What are you trying to show there?

Your breakpoint was still on the same line, and your query hasn't been executed yet. If your breakpoint was one line down, then the query would have run and you would be able to check the value of [kbd]result[/kbd] by hovering over the variable name, and expanding properties like a tree view.
 
my apologize I didn't understand what you meant.

see picture below:
2015_11_26_11_55_23_HAL_Statistics_2015_11_23_Debugging_Microsoft_Visual_Studio_Administrator__dumqnk.png


Thank you in advance.
2015_11_26_11_55_23_HAL_Statistics_2015_11_23_Debugging_Microsoft_Visual_Studio_Administrator__bvywud.png


2015-11-26_11_59_15-HAL_Statistics_2015-11-23_Debugging_-_Microsoft_Visual_Studio_Administrator_tukvzd.png
 
Perfect. That shows that the query is executing successfully and return a number of results.

Based on that, I see no reason why the [kbd]Console.WriteLine[/kbd] line wouldn't get hit. If you breakpoint that line does it get hit?
 
I think I have made something wrong because the "var result = tdc.ExecuteQuery" checks wrong context.

this is wrong:
var result = tdc.ExecuteQuery<company>(@"SELECT Id FROM CompanyMain");

this is correct:

var result = tdc.ExecuteQuery<myDbContext>(@"SELECT Id FROM CompanyMain");

hmm but when I change the code <company> to my context (myDbContext>
I get this error:

Cannot implicitly convert type 'System.Collections.Generic.List<context.contextDataContext>' to 'System.Collections.Generic.IEnumerable<MyProgram.Company>'. An explicit conversion exists (are you missing a cast?)


Could you help me?

Thank
 
In that example - [kbd]tdc[/kbd] is the context. What is in the brackets is the model/table you are querying.
 
Hi #Borvik,

If I have understood your question correct,
its table values(Id from the table CompanyMain)

ApMRJxK.png
 
Probably correct - but I think we are starting to get off topic here.

You never answered if the breakpoint on [kbd]Console.WriteLine[/kbd] gets hit. Does it?
 
I don't get any result from console.WriteLine (nothing)
 
I didn't ask if you got a result, I asked if the breakpoint got hit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top