INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

how do I return list?

how do I return list?

(OP)
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 --> C#

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


RE: how do I return list?

Two things:

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

Second, you are returning an IEnumerable. IEnumerable 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) 

RE: how do I return list?

(OP)
Hi #Borvik
this is my final code:
but I get the error:

No overload for method 'CompanyN' takes 0 arguments

CODE --> C#

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();
        }
    }
} 

RE: how do I return list?

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 http://www.microspostools.com/

RE: how do I return list?

Exactly the error I would have expected (see the "First" item in my other post). CathalMF 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(); 

RE: how do I return list?

(OP)
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..





Thank you in advance.

RE: how do I return list?

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 Console.WriteLine is never hit?

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

RE: how do I return list?

(OP)
Hi Borvik,




Thank you in advance

RE: how do I return list?

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 result by hovering over the variable name, and expanding properties like a tree view.

RE: how do I return list?

(OP)
my apologize I didn't understand what you meant.

see picture below:


Thank you in advance.


RE: how do I return list?

Perfect. That shows that the query is executing successfully and return a number of results.

Based on that, I see no reason why the Console.WriteLine line wouldn't get hit. If you breakpoint that line does it get hit?

RE: how do I return list?

(OP)
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

RE: how do I return list?

In that example - tdc is the context. What is in the brackets is the model/table you are querying.

RE: how do I return list?

(OP)
Hi #Borvik,

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

RE: how do I return list?

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

You never answered if the breakpoint on Console.WriteLine gets hit. Does it?

RE: how do I return list?

(OP)
I don't get any result from console.WriteLine (nothing)

RE: how do I return list?

I didn't ask if you got a result, I asked if the breakpoint got hit.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close