LINQ select all fields in table and set variables or class using a function?

Hi all,

i want to use a function to get all the fields from a LINQ table as ill be using this table alot i thought putting it in a function would be quicker

how can i get all the fields and all the field data from linq table and use it in a function

for example

var address1 = FunctionName.Address1
var postcode = functioname.postcode

or however i can make this work

ive started the function below but think im diong it way wrong thus far

Thanks for any help
public static string CompanyInfo()
        {
            using (CompanyDataContext Ci = new CompanyDataContext())
            {
                //get templates based on category
                var SelCompQuery = from C in Ci.CompanyInfos
                                        select C);

                foreach(var result in SelCompQuery) {

                }
            }
        }

Open in new window

LVL 1
awilderbeastAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John ClaesSenior .Net Consultant & Technical AnalistCommented:
SelCompQuery will contain a List (IQueryAble<>) of all yourCompanyInfo records/Objects

So looping through the list

foreach (YourDataTypeOfCompanyInformation companyInfo in SelCompQuery )
{
        // Here you can Access all companyInfo.
}

0
awilderbeastAuthor Commented:
ok so ive changed to

public static Iqueryable CompanyInfo() for the function name, that right?

what do i change the foreach to? is below right?

then what do i return?

also at the moment, select C is wrong it says, only assignment call, increment etc can be used as statement
public static IQueryable CompanyInfo()
        {
            using (CompanyDataContext Ci = new CompanyDataContext())
            {
                //get templates based on category
                var SelCompQuery = from C in Ci.CompanyInfos
                                        select C);

                foreach(var result in SelCompQuery) {
                    int ID = result.ID;
                    int ACK = result.ACK;
                    string CompanyName = result.CompanyName;

                }
            }
        }

Open in new window

0
awilderbeastAuthor Commented:
ok please tell me this is not the way to do it lol

this actually says not all code paths return a value, though i bet to differ!
and return ACT; says unreachable code detected

can anyone help me out?

Thanks
public static string CompanyInfo()
        {
            using (CompanyDataContext Ci = new CompanyDataContext())
            {
                //get templates based on category
                var SelCompQuery = from C in Ci.CompanyInfos
                                        select C;

                foreach(var result in SelCompQuery) {
                    string ID = Convert.ToString(result.ID);
                    string ACK = Convert.ToString(result.ACK);
                    string RMID = Convert.ToString(result.RMID);
                    string Sec1 = Convert.ToString(result.Sec1);
                    string Sec2 = Convert.ToString(result.Sec2);
                    string Sec3 = Convert.ToString(result.Sec3);
                    string Sec4 = Convert.ToString(result.Sec4);
                    string Sec5 = Convert.ToString(result.Sec5);                  
                    string CompanyType = result.CompanyType;
                    string OfficeTel = result.OfficeTel;
                    string OfficeFax = result.OfficeFax;
                    string Trades = result.Trades;
                    string CompanyReg =result.CompanyReg;
                    string CompanyName = result.CompanyName;
                    string Address1 = result.Address1;
                    string Address2 = result.Address2;
                    string Address3 = result.Address3;
                    string Postcode = result.Postcode;
                    string Website = result.Website;
                    string YearEstablished = result.YearEstablished;
                    string Constructionline = result.Constructionline;
                    string CHAS = result.CHAS;
                    string Notes = result.Notes;
                    string Locked = Convert.ToString(result.Locked);

                    return ID; return ACK; return RMID; return Sec1; return Sec2; return Sec3; return Sec4; return Sec5;
                    return CompanyType; return OfficeTel; return OfficeFax; return Trades; return CompanyReg;
                    return CompanyName; return Address1; return Address2; return Address3; return Postcode;
                    return Website; return YearEstablished; return Constructionline; return CHAS; return Notes;
                    return Locked;

                }
            }
        }

Open in new window

0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

John ClaesSenior .Net Consultant & Technical AnalistCommented:
Your First answer was the right one ;-)
// If you make the Ci.CompanyInfos return a list of CompanyInfo Objects

public static IQueryable<CompanyInfo>  GetCompanyInfo()
        {
            using (CompanyDataContext Ci = new CompanyDataContext())
            {
                //get templates based on category
                var SelCompQuery = from C in Ci.CompanyInfos select C);
                return SelCompQuery ;
            }
        }


Your second answer looks very weird......
at the end I see All Returns so it only works the first => ID
and the rest will never be executed



But just for you ;- )

on the CompanyDataContext you have something like this

        public IQueryable<CompanyInfo> CompanyInfos
        {
            get
            {
               return CreateQuery<CompanyInfo>("EntitiesContainer.CompanyInfos");                
            }
        }

You call this one on the presenter  using
public static IQueryable<CompanyInfo>  GetCompanyInfo()
        {
            using (CompanyDataContext Ci = new CompanyDataContext())
            {
                //get templates based on category
                var SelCompQuery = from C in Ci.CompanyInfos select C);
                return SelCompQuery ;
            }
        }

Now you have at your Client requested all the CompanyInfo objecten so you can bind them
or send the information to another Method


private void StartFunction()
{
           IQueryable<CompanyInfo> companyInfos = CompanyInfoPresenter.GetCompanyInfo()
           foreach (CompanyInfo companyInfo in companyInfos)
           {
                   //run the Mistery Function
                   MisteryFunction(companyInfo);
            }
}


in the MisteryFunction you can access all the data through the properties
0
awilderbeastAuthor Commented:

not quite following you...

so as in the code below those first two parts go together

this line  return CreateQuery<CompanyInfo>("EntitiesContainer.CompanyInfos");        
gives CreateQuery does not exist in the current context

and in my page i can use Code 1 to return any field or code 2 to put fields in another function

what is misteryfunction? that isnt defined anywhere?

Thanks
################# Functions CodeBehind ###########################
public IQueryable<CompanyInfo> CompanyInfos
        {
            get
            {
               return CreateQuery<CompanyInfo>("EntitiesContainer.CompanyInfos");                
            }
        }

        public static IQueryable<CompanyInfo>  GetCompanyInfo()
        {
            using (CompanyDataContext Ci = new CompanyDataContext())
            {
                //get templates based on category
                var SelCompQuery = from C in Ci.CompanyInfos select C;
                return SelCompQuery ;
            }
        } 
#################### Code 1 #############################
 
IQueryable<CompanyInfo> companyInfos = CompanyInfoPresenter.GetCompanyInfo();
                   foreach (CompanyInfo companyInfo in companyInfos)
                   {
                           //run the Mistery Function
                           MisteryFunction(companyInfo);
                    }

#################### Code 2 #############################
        private void StartFunction()
        {
                   IQueryable<CompanyInfo> companyInfos = CompanyInfoPresenter.GetCompanyInfo();
                   foreach (CompanyInfo companyInfo in companyInfos)
                   {
                           //run the Mistery Function
                           MisteryFunction(companyInfo);
                    }
        }

Open in new window

0
awilderbeastAuthor Commented:
hows this looking

i have no errors here now
if what ive done is right can you tell me how to get the strings from the codebehind so i can use them in the aspx page?

Thanks
################## FUNCTIONS #####################
public static IQueryable<CompanyInfo>  GetCompanyInfo()
                       {
                           using (CompanyDataContext Ci = new CompanyDataContext())
                           {
                               //get templates based on category
                               var SelCompQuery = from C in Ci.CompanyInfos select C;
                               return SelCompQuery ;
                           }
                       } 
################## Default.asp.cs #################
    protected void Page_Load(object sender, EventArgs e)
    {
        IQueryable<CompanyInfo> companyInfos = DB.Functions.GetCompanyInfo();
        foreach (CompanyInfo Result in companyInfos) {
            string StrID = Convert.ToString(Result.ID);
            string StrDateAdded = Convert.ToString(Result.DateAdded);
            string StrCompanyName = Result.CompanyName;
            string StrCompanyType = Result.CompanyType;
            string StrCompanyRef = Result.CompanyReg;
            string StrTrades = Result.Trades;
            string StrAddress = Result.Address1;
            string StrPostcode = Result.Postcode;
        }

    }

Open in new window

0
John ClaesSenior .Net Consultant & Technical AnalistCommented:
Part 1 gets all the Entities in your APP :
I use it to get the Context Filled

Code 1 will Return the Object itself so YOU can refer to the attributes

The MysteryFunction is your Function where you need the data in .


Example

You have in your context the CompanyInfos
You need all those and send them to a
CODE 2 : function PrintPostalCodes(string PostalCode)
CODE 1 : function PrintPostalCodes(CompanyInfo companyInfo )

then you can do the folowing: CODE 2
var SelCompQuery = from C in Ci.CompanyInfos select C
foreach (CompanyInfo companyInfo in SelCompQuery)
{
               PrintPostalCodes(companyInfo.PostalCode);
}

Or you can Do the folowing : CODE1
var SelCompQuery = from C in Ci.CompanyInfos select C
foreach (CompanyInfo companyInfo in SelCompQuery)
{
               PrintPostalCodes(companyInfo);
}
0
awilderbeastAuthor Commented:
i think im sort of following you

the way i have it above then, that will work?

how do i get the strings i declared in the codebehing to the aspx page?

Thanks
0
John ClaesSenior .Net Consultant & Technical AnalistCommented:
your change should be working.
0
John ClaesSenior .Net Consultant & Technical AnalistCommented:
but instead of sending the values to a string
why not directly ot the controller? ?

Your code
string StrCompanyName = Result.CompanyName;

My proposal
companyNameTextBox.Text = Result.CompanyName;

regards

poor beggar
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awilderbeastAuthor Commented:
i just wanted to know how to do it incase i needed to not use a controller?
0
awilderbeastAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.