البرمجة

تحويل استعلام SQL إلى LINQ في C#

To convert your SQL query to LINQ in C#, you can use the following LINQ query. Make sure to replace ProjectId with the actual variable or value you want to use.

csharp
var result = ( from ts in Db.Tasks join prt in Db.ProjectTasks on ts.Id equals prt.TaskId join tl in Db.Timeline on ts.Id equals tl.TypeId into timelineGroup from t2 in timelineGroup.DefaultIfEmpty() join dur in Db.Duration on ts.Id equals dur.TypeId into durationGroup from t3 in durationGroup.DefaultIfEmpty() where prt.ProjectId == ProjectId && (t2 == null || t2.Type == (int)Provider.EntityType.TASK) && (t3 == null || t3.Type == (int)Provider.EntityType.TASK) select new { Id = "T" + ts.Id, ts.Name, ts.Description, ts.Priority, ts.Stage, ts.Status, ts.CreatorId, ProjectId = prt.ProjectId, StartDate = t2 != null ? t2.StartDate : null, EndDate = t2 != null ? t2.EndDate : null, LatestEndDate = t2 != null ? t2.LatestEndDate : null, LatestStartDate = t2 != null ? t2.LatestStartDate : null, EarliestStartDate = t2 != null ? t2.EarliestStartDate : null, ActualStart = t2 != null ? t2.ActualStart : null, ActualEnd = t2 != null ? t2.ActualEnd : null, RemTime = t2 != null ? t2.RemTime : null, ReshowDate = t2 != null ? t2.ReshowDate : null, Completed = t2 != null ? t2.Completed : null, ActualDuration = t3 != null ? t3.ActualDuration : null, ActualDurationPlanned = t3 != null ? t3.ActualDurationPlanned : null } ).ToList();

This LINQ query will perform the equivalent of your SQL query, including the CONCAT function and the left joins. It also handles cases where the joined tables may not have matching records, which would result in null values in the corresponding properties of the result objects.

المزيد من المعلومات

الكود السابق يقوم بتحويل الاستعلام SQL الخاص بك إلى استعلام LINQ في C#. هذا الاستعلام يقوم بالتالي:

  1. يقوم بالانضمام (join) بين جدول Tasks وجدول ProjectTasks بناءً على مطابقة Id مع TaskId.
  2. يقوم بالانضمام اليساري (left join) بين جدول Tasks وجدول Timelines بناءً على مطابقة Id مع TypeId ويحد النتائج للحالات حيث Type يساوي 3.
  3. يقوم بالانضمام اليساري (left join) بين جدول Tasks وجدول Durations بناءً على مطابقة Id مع TypeId ويحد النتائج للحالات حيث Type يساوي 3.
  4. يقوم بتحديد العمود الناتج Id باستخدام دالة CONCAT لإضافة حرف “T” قبل قيمة Id من الجدول Tasks.
  5. يحدد الخصائص الأخرى المطلوبة من الجدول Tasks والجداول المنضمة مع التعامل مع القيم الفارغة (null) في حالة عدم وجود تطابق في الجداول المنضمة.

يرجى التأكد من تعديل المتغير ProjectId في الاستعلام بقيمة فعلية أو متغير يحمل القيمة المطلوبة لتنفيذ الاستعلام بنجاح.

زر الذهاب إلى الأعلى