none
Select multiple columns (basic) RRS feed

  • Question

  • Hello, I am quite new to LINQ, and I am stuck with this probably basic problem, but haven't been able to find a solution, so I thought I could ask here.

     

    So, I have this repository where I put my functions for data accessing, and now I need to write a function to get all the applicants that have applied for this particular advertisement, and their values too. Now, the values are stored in a separate M:N table, where every row consists of the applicantID,advertisementID and value. I just wanna append the value to every applicant that I show, so that it would look like a Ranking List. 

     

    However, I do not seem to know how to select just the name column from the Applicant table, and just the Value column from the ApplicantValue table.

     

    And there's this Iqueryable interface that further confuses me...

     

    Any help will be appreciated. 

     

    Here's my function to get the Applicants only:

     

     public IQueryable<Applicant> getApplicantsAndValuesForAdvertisement(int id)
        {
          return from appValues in entities.ApplicantValues
              from app in entities.Applicants
              where app.applicantID == appValues.appID
              && appValues.advID == id
              select app;
              
        }
    

    Now, if I try to add to the definition of the type two columns, it says that it only messes with types, i.e. one type should be passed. So I figured I would do a ViewModel and then pass it, like this

    public class ApplicantRankingViewModel
      {
        public Applicant Applicant { get; set; }
        public Int32 Value { get; set; }
    
        public ApplicantRankingViewModel(Applicant app, Int32 value)
        {
          Applicant = app;
          Value = value;
        }
      }
    

    and then in the function I have no idea how to pass the values:

    public IQueryable<ApplicantRankingViewModel> getApplicantsAndValuesForAdvertisement(int id)
        {
          return from appValues in entities.ApplicantValues
              from app in entities.Applicants
              where app.applicantID == appValues.appID
              && appValues.advID == id
              select new { app, appValues.value };
              
        }
    

     

    Wednesday, February 2, 2011 4:22 PM

Answers

  • Now, if I try to add to the definition of the type two columns, it says that it only messes with types, i.e. one type should be passed. So I figured I would do a ViewModel and then pass it, like this

     

    public class ApplicantRankingViewModel
    
     {
    
      public Applicant Applicant { get; set; }
    
      public Int32 Value { get; set; }
    
    
    
      public ApplicantRankingViewModel(Applicant app, Int32 value)
    
      {
    
       Applicant = app;
    
       Value = value;
    
      }
    
     }
    
    

     

    and then in the function I have no idea how to pass the values:

     

    public IQueryable<ApplicantRankingViewModel> getApplicantsAndValuesForAdvertisement(int id)
    
      {
    
       return from appValues in entities.ApplicantValues
    
         from app in entities.Applicants
    
         where app.applicantID == appValues.appID
    
         && appValues.advID == id
    
         select new { app, appValues.value };
    
         
    
      }
    
    

     

     


    Hi,

    The final "select new { app, appValues.value }" is an anonymous type, but you are wanting to return a set of ApplicantRankingViewModels, so change it to:

        "select new ApplicantRankingViewModel (app, appValues.value)"

    I think that should still be ok within the IQueryable<> wrapper.

    Thursday, February 10, 2011 4:14 PM