?
Solved

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

Posted on 2011-02-23
12
Medium Priority
?
637 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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
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

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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