locked
how can I select the column from the joining table linq RRS feed

  • Question

  • User-1355965324 posted

    Hi

    I am using the following Linq query  with join. I want to select all the column from d and c.DepotName into the list variable  data.  How can I select into c.depotname into the variable data , Please help

    public ICollection<MeterReadingModel> GetReadingforTwoDates(int depotno,int year, int month)
            {
    
                var data = (from d in ctx.goMeterReading
                            join c in ctx.goDepot on d.DepotNo equals c.DepotNo
                            where d.DepotNo == depotno &&  d.ReadingYear == year && d.ReadingMonth == month
                            select  d
                            ).ToList();
                return data;  
            }
    

    With many thanks

    Pol

    Tuesday, November 12, 2019 2:18 PM

Answers

  • User475983607 posted

    Simply add DepotName.  This assumes your goMeterReqading has a DepotName  column, the join works and there is data that meets the filter.

     select  d.DepotName 

    You'll also need to update the action's return value to match;  List<string>.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 12, 2019 2:25 PM
  • User475983607 posted

    IMHO, you have been on these forums far too long to post such a vague response...

    What does "not working" mean?  Did you receive an error?  Your original post states you want to select a column but you selected an entire object and column.  Why?  Seems you are not using the debugger?

    var data = (from d in ctx.goMeterReading
                            join c in ctx.goDepot on d.DepotNo equals c.DepotNo
                            where d.DepotNo == depotno &&  d.ReadingYear == year && d.ReadingMonth == month
                            select c.DepotName
                            ).ToList();
                return data;

    Also you did not post all the relevant code.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 12, 2019 3:18 PM
  • User-1355965324 posted

    I corrected the code  the way given

    ar data = (from d in ctx.goMeterReading
                            join c in ctx.goDepot on d.DepotNo equals c.DepotNo
                            where d.DepotNo == depotno &&  d.ReadingYear == year && d.ReadingMonth == month
                            select new MeterReadingModel
                            {
                                DepotNo= c.DepotNo,
                                DepotName=c.DepotName,
                                MeterCode = d.MeterCode,
                                ReadingYear = d.ReadingYear,
                                ReadingMonth=d.ReadingMonth,
                                Reading = d.Reading
                            }
                            ).ToList();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 12, 2019 3:39 PM

All replies

  • User475983607 posted

    Simply add DepotName.  This assumes your goMeterReqading has a DepotName  column, the join works and there is data that meets the filter.

     select  d.DepotName 

    You'll also need to update the action's return value to match;  List<string>.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 12, 2019 2:25 PM
  • User-1355965324 posted

    I did the same way as given below but not working

    var data = (from d in ctx.goMeterReading
                            join c in ctx.goDepot on d.DepotNo equals c.DepotNo
                            where d.DepotNo == depotno &&  d.ReadingYear == year && d.ReadingMonth == month
                            select  d,c.DepotName
                            ).ToList();
                return data;  

    Tuesday, November 12, 2019 2:52 PM
  • User475983607 posted

    IMHO, you have been on these forums far too long to post such a vague response...

    What does "not working" mean?  Did you receive an error?  Your original post states you want to select a column but you selected an entire object and column.  Why?  Seems you are not using the debugger?

    var data = (from d in ctx.goMeterReading
                            join c in ctx.goDepot on d.DepotNo equals c.DepotNo
                            where d.DepotNo == depotno &&  d.ReadingYear == year && d.ReadingMonth == month
                            select c.DepotName
                            ).ToList();
                return data;

    Also you did not post all the relevant code.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 12, 2019 3:18 PM
  • User-1355965324 posted

    I corrected the code  the way given

    ar data = (from d in ctx.goMeterReading
                            join c in ctx.goDepot on d.DepotNo equals c.DepotNo
                            where d.DepotNo == depotno &&  d.ReadingYear == year && d.ReadingMonth == month
                            select new MeterReadingModel
                            {
                                DepotNo= c.DepotNo,
                                DepotName=c.DepotName,
                                MeterCode = d.MeterCode,
                                ReadingYear = d.ReadingYear,
                                ReadingMonth=d.ReadingMonth,
                                Reading = d.Reading
                            }
                            ).ToList();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 12, 2019 3:39 PM