使用LINQ

时间:2018-03-24 17:27:55

标签: linq

我正在尝试编写一个查询来连接两个表并编写客户端名称     最大利润按降序排列。     课程如下 -     {     公共类客户     {         public int ClientId {get;组; }

    public string ClientName { get; set; }
    public string StreetAddress { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string Telephone { get; set; }
}
}
{
public class Project
{
    public int ProjectId { get; set; }

    public string ProjectName { get; set; }
    public DateTime StartProjectDate { get; set; }
    public DateTime DeliverProjectDate { get; set; }
    public int Profit { get; set; }

    public Client Client { get; set; }

}

}
and my query is -
ProjectContext p = new ProjectContext();
        var res = p.Projects
                   .Include(y => y.Client.ClientName)
                   .Join(p.Clients,
                   x => x.Client.ClientId,
                   c => c.ClientId,
                   (x, c) => new { x, c })
                   .OrderByDescending(p.Profit)
                   .Take(4);
        Console.WriteLine("The four most profitable clients to date are:");

        foreach(var i in res)
        {
            Console.WriteLine(y.Client.ClientName);
        }

    It is throwing an error in the .OrderByDescending statement using profit 
    and the foreach statement stating that cannot operate on variables of 
    type ? because ? does not contain a public definition for GetEnumerator, 
    and the WriteLine is incorrect for ClientName.  

Any help would be greatly appreciated!!

Thanks,
Joe

1 个答案:

答案 0 :(得分:0)

包括Client,而不是ClientName

IEnumerable<Client> profitableClients = context.Projects
    .Include(proj => proj.Client)
    .OrderByDescending(proj => proj.Profit)
    .Select(proj => proj.Client)
    .Take(4);