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

x
?
Solved

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

Posted on 2011-02-23
12
Medium Priority
?
639 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:awilderbeast
[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
  • 7
  • 5
12 Comments
 
LVL 10

Expert Comment

by:John Claes
ID: 34961370
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34961468
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34962446
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 10

Expert Comment

by:John Claes
ID: 34968125
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34968321

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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34971207
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
 
LVL 10

Expert Comment

by:John Claes
ID: 34971413
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34971518
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
 
LVL 10

Expert Comment

by:John Claes
ID: 34971717
your change should be working.
0
 
LVL 10

Accepted Solution

by:
John Claes earned 1500 total points
ID: 34971727
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34971805
i just wanted to know how to do it incase i needed to not use a controller?
0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 35019566
thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

609 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