[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 645
  • Last Modified:

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

0
awilderbeast
Asked:
awilderbeast
  • 7
  • 5
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
awilderbeastAuthor Commented:
i just wanted to know how to do it incase i needed to not use a controller?
0
 
awilderbeastAuthor Commented:
thanks
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now