lambda expression using max() like SQL query

CC 321 Reputation points
2021-07-01T00:08:51.163+00:00

I have this TSQL I need to convert Lambda expression:

SELECT max(WorkPackage.ID) FROM Package
WHERE (Package.WPTitle LIKE 'TWF fac%') AND (Package.Status = 'Baseline') group by WBSID)

I have this so far, but it doesn't use the max() in my TSQL:
var dblist = db.Packages.Where(i => i.WPTitle.Contains("TWF") && i.Status.Equals("Baseline").GroupBy(i => i.WBSID);)
;

How can I use Max() in my Lambda expression?

This throws an exception on the Max():
var dbList = db.WorkPackages
.Where(i => i.WPTitle.Contains("TWF") && i.Status.Equals("Baseline"))
.GroupBy(x => x.WBSID)
.Max();

Developer technologies Transact-SQL
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. CC 321 Reputation points
    2021-07-01T12:59:21.123+00:00

    Got it to work with the following as I needed to utilize another table:

    var dbList = db.Packages.Join(db.WBS,
    wp => wp.WBSID,
    wbs => wbs.ID,
    (wp , wbs) => new { wp.ID , wp.WPTitle , wp.Status, wp.WBSID, wbs.WBSNum})
    .Where(i => i.WPTitle.Contains("TWF") && i.Status.Equals("Baseline"))
    .GroupBy(x => x.WBSNum).Select(grp => grp.Max(c => c.ID));

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-07-01T02:42:49.797+00:00

    Try something like this:

     class Program  
    {  
        static void Main(string[] args)  
        {  
            using (Model1 model1 = new Model1())  
            {  
                // Query Syntax  
                var re = from score in model1.scores.ToList()  
                         group score by score.ClassID into g  
                         select new  
                         {  
                             Max = g.Select(s=> s.Score1).Max()  
                         };  
                 //Method syntax  
                 var re1 = model1.scores.ToList().GroupBy(s => s.ClassID).SelectMany(s => s.Where(i => i.Score1 == s.Max(j => j.Score1))).Select(s=>s.Score1);  
    
                Console.WriteLine();  
            }  
        }  
    }  
    [Table("score")]  
    public partial class score  
    {  
        [DatabaseGenerated(DatabaseGeneratedOption.None)]  
        public int Id { get; set; }  
    
        public int? ClassID { get; set; }  
    
        [StringLength(10)]  
        public string Name { get; set; }  
    
        [Column("Score")]  
        public double? Score1 { get; set; }  
    }  
    

    Both query syntax and method syntax are available, and you can choose the way you like.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.