Home > Mobile >  Return value Of LINQ Query inside Repository
Return value Of LINQ Query inside Repository

Time:01-03

I Create Method inside InterFace like This List<T> OngoingEvent();

and I implemented the Method Inside Repository

public List<T> OngoingEvent()
{
    var output2 = DbContext.ParticipantsTable.Join(DbContext.EventTable, e => e.EventTableID, d => d.ID,
         (tbl1, tbl2) => new
         {
             Table1 = tbl1,
             Table2 = tbl2
         }).Join(DbContext.VolunteerTable, ee => ee.Table1.VolunteerTableID, dd => dd.ID,
         (tbl1, tbl2) => new
         {
             ID = tbl1.Table1.ID,
             IsAccepted = tbl1.Table1.IsAccepted,
             IsAttend = tbl1.Table1.IsAttend,
             EventName = tbl1.Table2.EventName,
             EventEndDate = tbl1.Table2.EventEndDate,
             VolunteerName = tbl2.VolunteerName
         }).ToList();

    return output2;

}

but I get Error for the return value

Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: int Id, bool Accepted, bool Attend, string EventName, string EventEndDate, string VolunteerName>>' to 'System.Collections.Generic.List'

I Try To change List<T> OngoingEvent(); to IEnumerable OngoingEvent();`

and IQueryable OngoingEvent();` but Not working

and I try to use Method like this IQueryable OngoingEvent(Expression<Func<T, bool>> expression); but o don't know how i can deal with it

CodePudding user response:

The error is telling you that the return type of a list of output2 which is an anonymous object by virtue of using:

new
{
    DynamicProp1Name = value1,
    DynamicProp2Name = value2,
    ... etc ...
}

cannot be converted to a list of type T.

You could solve your problem quickly by changing the signature to either:

public List<object> OngoingEvent();

or

public List<dynamic> OngoingEvent();

However, I would go for defining a class that represents the return type:

public class OngoingEventResult
{
    public int ID { get; set; }
    public bool IsAccepted { get; set; }
    public bool IsAttend { get; set; }
    public string EventName { get; set; }
    public DateTime EventEndDate { get; set; }
    public string VolunteerName { get; set; }
}

and then using this as the return type:

public List<OngoingEventResult> OngoingEvent()
{
    ...
    (tbl1, tb2) => new OngoingEventResult()
    {
        ID = tbl1.Table1.ID,
        IsAccepted = tbl1.Table1.IsAccepted,
        IsAttend = tbl1.Table1.IsAttend,
        EventName = tbl1.Table2.EventName,
        EventEndDate = tbl1.Table2.EventEndDate,
        VolunteerName = tbl2.VolunteerName    
    }
    ...
}
  • Related