0

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

1 Answer 1

1

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 } ... } 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Neil W To help me I try to Change in the inerface List<object> OngoingEvent(); and i change in repository List<ParticipantsTable> OngoingEvent() {.. return output2; } buu still get Error Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: int ID, bool IsAccepted, bool IsAttend, string EventName, string EventEndDate, string VolunteerName>>' to 'System.Collections.Generic.List<object>'
If you're still using the anonymous type but want to return a list of object, instead of creating an OngoingEventResult, then you'll need to finish with .ToList<object>().
However, I still strongly recommend using a defined class such as OngoingEventResult instead of using anonymous and object or dynamic.
Thanks a lot Neil it's works when i use ToList<object>() i was defined three class
Thanks a lot Neil it's works when i use ToList<object>() i was create three model classes ParticipantsTable EventTable VolunteerTable and the query uses This three tables and i have three folder models (to define three tables above ) interface (List<object> OngoingEvent();) repository (the query above ) do i on the right track

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.