locked
Linq statement error variable undeclared RRS feed

  • Question

  • User442275730 posted

    I am trying to get a bit of linq working so I am but its saying the variable is undeclared

    Cannot use local variable 'q' before it is declared

    public List<tblPersonal> getPersonalInfo(Guid personalId)
    {
       List<tblPersonal> list = new List<tblPersonal>(); 
       try
          {
              var q = from  tblIncome in _apertureNetContext.apertureNetEntities.tblIncomes                        
              select q;
    
                     
           }
           catch (Exception ex)            
           {
    
           }
    
    
          return list;
    }
    

    Wednesday, February 24, 2016 12:07 PM

Answers

  • User603616845 posted

    Hi,

    You need to use your linq query as following.

    var q = from  tblIncome in _apertureNetContext.apertureNetEntities.tblIncomes                        
              select tblIncome ;

    In the above linq query, you are getting the data from your database's table in instance "tblIncome". And this value is assign to "q" which is declared as "var q". You can use to get all the column with some other property like following example.

    string name=q.Name.ToString();
    string address=q.Address.ToString();

    Hope this will help you.

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2016 12:28 PM
  • User-698989805 posted

    Hello @david38ni!! You should be doing the following:

     public static List<CategoryDetail> GetCategoryFullDetails()
        {
           using (DemoEntities db = new DemoEntities()) //Database connection or connection instance
             {
                var con = (from c in db.Category //'con' is a variable in which you can say, assigned the query
                           select c).ToList(); //Finally with ToList() method, you retrieve the value from database
    return con; } }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2016 5:09 PM

All replies

  • User603616845 posted

    Hi,

    You need to use your linq query as following.

    var q = from  tblIncome in _apertureNetContext.apertureNetEntities.tblIncomes                        
              select tblIncome ;

    In the above linq query, you are getting the data from your database's table in instance "tblIncome". And this value is assign to "q" which is declared as "var q". You can use to get all the column with some other property like following example.

    string name=q.Name.ToString();
    string address=q.Address.ToString();

    Hope this will help you.

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2016 12:28 PM
  • User-698989805 posted

    Hello @david38ni!! You should be doing the following:

     public static List<CategoryDetail> GetCategoryFullDetails()
        {
           using (DemoEntities db = new DemoEntities()) //Database connection or connection instance
             {
                var con = (from c in db.Category //'con' is a variable in which you can say, assigned the query
                           select c).ToList(); //Finally with ToList() method, you retrieve the value from database
    return con; } }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2016 5:09 PM
  • User1124521738 posted

    you have declared a variable of q but then tried to select it within the linq statement, try this instead.

    This uses q as the alias for your linq table, and assigns the output to your list variable.

    public List<tblPersonal> getPersonalInfo(Guid personalId)
    {
       List<tblPersonal> list = new List<tblPersonal>(); 
       try
          {
              list  = (from q in _apertureNetContext.apertureNetEntities.tblIncomes                        
                         select q).ToList();
           }
           catch (Exception ex)            
           {
    
           }
    
    
          return list;
    }

    Wednesday, February 24, 2016 5:54 PM