Labels

Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Thursday, June 25, 2015

Populating a List using LINQ

List<string> PayFrequency = new List<string>();

using (var con = new OleDbConnection(Connections.XYZDB.ConnectionString.ToString()))
            {
con.Open();
using (var cmdPF = new OleDbCommand(getPFQuery, con))
                {
                    using (var rdrPF = cmdPF.ExecuteReader())
                    {
                        PayFrequency = (from IDataRecord rPF in rdrPF
                                        select (string)rPF["ImportFrequencyCode"]
                                         ).ToList();
                    }
                }
}

Querying the List of class objects using LINQ

public string GetExceptionMessage(Int16 messageID, List<ExceptionMessage> exMsg)
        {

            var exceptionMessage = (from em in exMsg
                                    where em.ExceptionMessageID == messageID
                                    select em.ExceptionMsg).FirstOrDefault();

            return string.IsNullOrWhiteSpace(exceptionMessage) ? string.Empty : exceptionMessage;
        }