Reputation: 55
I have this model with three collections of the same model <AttendeesMeeting>
Meeting
[NotMapped]
[Display(Name = "xxxx", ResourceType = typeof(Resources.ReuniaoResources))]
public virtual ICollection<AttendeesMeeting> Required{ get; set; }
[NotMapped]
[Display(Name = "xxx", ResourceType = typeof(Resources.ReuniaoResources))]
public virtual ICollection<AttendeesMeeting> Informed{ get; set; }
[NotMapped]
[Display(Name = "xxxx", ResourceType = typeof(Resources.ReuniaoResources))]
public virtual ICollection<AttendeesMeeting> Optionals{ get; set; }
In a method do a get to retrieve some values.
I'd like three of my model objects only receive the 'Login' from the returned values.
public Meeting GetReuniaoForEdit(int id)
{
var model = this.context.Meetings.Find(id);
var required = context.AttendeesMeeting.Where(x => x.IdReuniao == id && x.TypeAttendee == 1);
var informed = context.AttendeesMeeting.Where(x => x.IdReuniao == id && x.TypeAttendee == 2);
var optionals = context.AttendeesMeeting.Where(x => x.IdReuniao == id && x.TypeAttendee == 3);
if (required.Any() || informed.Any() || optionals.Any())
{
//Login is a string
model.required = required.Select(x => x.Login).ToList();
}
}
Error:
model.required = required.Select(x => x.Login).ToList();
Cannot implicitly convert type '
System.Collections.Generic.List<string>
' to 'System.Collections.Generic.ICollection<Models.AttendeesMeeting>
'. An explicit conversion exists (are you missing a cast?)
How do I turn the string list in a collection of my model?
It's possible?
Upvotes: 1
Views: 3360
Reputation: 22421
Right now, by .Select(x => x.Login).ToList()
you create a list of strings that you want to assign to a ICollection<AttendeesMeeting>
. You can solve this in several ways, for instance:
ICollection<AttendeesMeeting>
is substituted by ICollection<string>
. If you ever only need the Login
, then your model should be optimized for that. .Select(x => x.Login)
and by that assign the collection of meetings to the property in the model; in your view, you only show the Login
property of the model and leave out the rest of the data.ICollection<string>
to the new model and use this in the special case. Upvotes: 2
Reputation: 37000
I guess what you want is get the meetings by their login
. Thus you need a further step as required.Select(x => x.Login).ToList()
will only select the logins of your meetings. However you need to further select all those meetings that fit those IDs:
var model = this.context.Meetings.Where(x => required.Select(y => y.Login).Contains(x.login));
Upvotes: 0