Return var from function

10,728

Solution 1

var isn't a type in itself. It just asks the compiler to infer the type of the local variable.

Now in your case, the type is a List<T> where the T is an anonymous type. If you want to be able to use the properties within the elements of the list from other code, you'll need to either do so dynamically (ick) or turn the anonymous type into a full, named type. Anonymous types are really only designed to be used from the methods where the objects are created. You can then return a List<DenormalizedRecord> or whatever.

Also note that your query would be much simpler if you'd just use joins:

 from record in c.GetTable<T_RECORDSHOW>()
 where record.RecordStatus.Equals(RecordStatus.Active)
 join student in c.T_STUDENTSHOWs on record.StudentId equals student.Id
 join trade in c.T_TRADESHOWs on record.TradeId equals trade.Id
 join session in c.T_SESSIONSHOWs on record.SessionId equals session.Id
 select new DenormalizedRecord {
     Student = student.Name,
     Trade = trade.Name,
     SessionId = session.Name, // Confusing property name, by the way
     Month = record.Month.ToString(), // Why the ToString()?
     Attendance = record.Attendance.ToString() // What the ToString()?
 }

Solution 2

It's not totally true that you cannot return anonymous types from a method, and I don't mean using tricky stuff with reflection. You just have to move the burden of instantiation to the caller:

IEnumerable<T> getlist<T>(Func<string, string, string, string, string, T> resultor)
{
    try
    {
        using (ShowDataToClientDataContext c = new ShowDataToClientDataContext())
        {
           var recList = (from record in c.GetTable<T_RECORDSHOW>()
                          where record.RecordStatus.Equals(RecordStatus.Active)
                          select resultor
                          (
                              (from stu in c.T_STUDENTSHOWs
                                         where stu.Id.Equals(record.StudentId)
                                         select stu.Name).Single().ToString(),
                              (from t in c.T_TRADESHOWs
                                       where t.Id.Equals(record.TradeId)
                                       select t.Name).Single().ToString(),
                              (from s in c.T_SESSIONSHOWs
                                           where s.Id.Equals(record.SessionId)
                                           select s.Name).Single().ToString(),
                              record.Month.ToString(),
                              record.Attendance.ToString()
                          )).ToList();
           return recList;
        }

    }
    catch
    {

    }
}

Type inference works like a charm, so you can call your method like this:

var list = getlist((st, tr, sid, m, att) => new 
{ 
    Student = st,
    Trade = tr,
    SessionId = sid,
    Month = m,
    Attendance = att 
});

No need to define any DTO class just for the sake of outputting those results.

PS: the query itself could be better, but I'm just tackling the problem in your question.

Share:
10,728
mitali
Author by

mitali

Updated on June 14, 2022

Comments

  • mitali
    mitali almost 2 years

    I am getting values from different tables i var type and I want to return them. What should be the return type of the function:-

      public void getlist()
        {
            try
            {
                using (ShowDataToClientDataContext c = new ShowDataToClientDataContext())
                {
                   var recList = (from record in c.GetTable<T_RECORDSHOW>()
                                  where record.RecordStatus.Equals(RecordStatus.Active)
                                  select new
                                  {
                                      Student = (from stu in c.T_STUDENTSHOWs
                                                 where stu.Id.Equals(record.StudentId)
                                                 select stu.Name).Single().ToString(),
                                      Trade = (from t in c.T_TRADESHOWs
                                               where t.Id.Equals(record.TradeId)
                                               select t.Name).Single().ToString(),
                                      SessionId = (from s in c.T_SESSIONSHOWs
                                                   where s.Id.Equals(record.SessionId)
                                                   select s.Name).Single().ToString(),
                                      Month = record.Month.ToString(),
                                      Attendance = record.Attendance.ToString(),
    
                                  }).ToList();
                   return recList;
                }
    
            }
            catch
            {
    
            }
        }
    

    anybody there to help me?