@Brian , you could try the following code to convert 2nd part to linq statement.
static void Main(string[] args)
{
SqlConnection connection = new SqlConnection("connstr");
connection.Open();
string sqltext = File.ReadAllText("D:\\Data.txt"); //I stored the first part to the txt file
SqlDataAdapter adapter = new SqlDataAdapter(sqltext, connection);
DataSet set = new DataSet();
adapter.Fill(set);
DataTable table = set.Tables[0];
var efwl = from data in table.AsEnumerable()
where data.Field<string>("Pre_Status") == "Event Full" || data.Field<string>("Pre_Status") == "Waiting List"
select new
{
ID = data.Field<object>("ID"),
Pre_Status = data.Field<object>("Pre_Status"),
Post_Status = data.Field<object>("Post_Status")
};
var enrolled = from data in table.AsEnumerable()
where data.Field<string>("Pre_Status") == "Enrolled"&& efwl.Select(i=>i.ID).Contains(data.Field<object>("ID"))
select new
{
ID = data.Field<object>("ID"),
Pre_Status = data.Field<object>("Pre_Status"),
Post_Status = data.Field<object>("Post_Status")
};
var Incomplete = enrolled.Where(i => i.Post_Status.ToString() == "Incomplete");
var complete= enrolled.Where(i => i.Post_Status.ToString() == "Complete");
var results1 = (from t1 in Incomplete
where !(from t2 in complete
select t2.ID).Contains(t1.ID)
select t1.ID).Distinct();
var Noshow = enrolled.Where(i => i.Post_Status.ToString() == "No Show");
var comandnotcom = enrolled.Where(i => i.Post_Status.ToString() == "Complete" || i.Post_Status.ToString() == "Incomplete");
var results2 = (from t1 in Noshow
where !(from t2 in comandnotcom
select t2.ID).Contains(t1.ID)
select t1.ID).Distinct();
var final = new Result
{
Total = efwl.Count(),
EventFull = efwl.Where(i => i.Pre_Status.ToString() == "Event Full").Count(),
WaitingList = efwl.Where(i => i.Pre_Status.ToString() == "Waiting List").Count(),
UniqueIndividuals = efwl.Select(i => i.ID).Distinct().Count(),
NotAdmitted = efwl.Select(i => i.ID).Except(enrolled.Select(i => i.ID)).Count(),
Enrolled = enrolled.Select(i => i.ID).Distinct().Count(),
Complete = enrolled.Where(i => i.Post_Status.ToString() == "Complete").Select(i => i.ID).Distinct().Count(),
Incomplete = results1.Count(),
NoShow = results2.Count(),
Cancelled = table.AsEnumerable().Where(i => i.Field<string>("Pre_Status") == "Cancelled" && enrolled.Select(m=>m.ID).Contains(i.Field<object>("ID"))).Select(i => i.Field<object>("ID")).Distinct().Count()
};
public class Result
{
public int Total { get; set; }
public int EventFull { get; set; }
public int WaitingList { get; set; }
public int UniqueIndividuals { get; set; }
public int NotAdmitted { get; set; }
public int Enrolled { get; set; }
public int Complete { get; set; }
public int Incomplete { get; set; }
public int NoShow { get; set; }
public int Cancelled { get; set; }
}
}
Result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.