@Murilo Junqueira , Welcome to Microsoft Q&A, based on my test, I find that your code is almost correct. What error you get from the above code?
I used the following code to make a test:
internal class Program
{
static void Main(string[] args)
{
List<Port> list1 = new List<Port>();
list1.Add(new Port() { Id=1001, color=Color.Red});
list1.Add(new Port() { Id = 1002, color = Color.Black });
list1.Add(new Port() { Id = 1003, color = Color.DarkRed });
List<LicsTotalModel> list2 = new List<LicsTotalModel>();
list2.Add(new LicsTotalModel() { idportal=1002, datapublicacao= System.DateTime.Now.Date.AddDays(-2) });
var result = list2.Where(l => l.datapublicacao >= System.DateTime.Now.Date.AddDays(-2))
.GroupBy(l => new { l.nomeportal, l.idportal })
.Select(l => new LicsTotalModel
{
idportal = l.Key.idportal,
nomeportal = l.Key.nomeportal,
color = list1.Where(g => g.Id == l.Key.idportal).Select(p => p.color).FirstOrDefault(), // <= ERROR HERE
}).ToList();
}
}
public class Port
{
public int Id { get; set; }
public Color color { get; set; }
}
public class LicsTotalModel
{
public int idportal { get; set; }
public int nomeportal { get; set; }
public DateTime datapublicacao { get; set; }
public Color color { get; set; }
}
I can get the result as the following: