Solved

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

Posted on 2011-02-23
12
593 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
  • 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 500 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now