Is there a better way to do this Linq query and function? - c#

I'm making this method retrieve records from the Data Base. As you can see I want to return a List<ITieneID> where ITieneID is an Interface defined on my business layer.
AtlasWFM_Entities.Clases.Area implements that interface. It is pretty clear that this is not a good way to accomplishing it. Here's the code:
public override List<ITieneID> Buscar(ITieneID elementoPatron)
{
List<ITieneID> result = new List<ITieneID>();
var resultado = from a in base.Repository.Context.Areas
where a.areaID.Equals(elementoPatron.ID) || a.areaDescripcion.Contains(elementoPatron.Descripcion)
select new AtlasWFM_Entities.Clases.Area
{
ID = a.areaID,
Descripcion = a.areaDescripcion,
Estado = a.areaEstado,
};
foreach (var r in resultado)
{
ITieneID t = new AtlasWFM_Entities.Clases.Area
{
ID = r.ID,
Descripcion = r.Descripcion,
Estado = r.Estado,
};
result.Add(t);
}
return result;
}
Any Ideas how to improve this?

public override List<ITieneID> Buscar(ITieneID elementoPatron)
{
var resultado = from a in base.Repository.Context.Areas
where a.areaID.Equals(elementoPatron.ID) || a.areaDescripcion.Contains(elementoPatron.Descripcion)
select new AtlasWFM_Entities.Clases.Area
{
ID = a.areaID,
Descripcion = a.areaDescripcion,
Estado = a.areaEstado,
};
return new List<ITieneID>(resultado);
}
UPDATE: That don't compile, but this should:
return new List<ITieneID>(resultado.Cast<ITieneID>());
In fact, I think you can reduce the whole thing to:
public override List<ITieneID> Buscar(ITieneID elementoPatron)
{
var resultado = from a in base.Repository.Context.Areas
where a.areaID == elementoPatron.ID || a.areaDescripcion.Contains(elementoPatron.Descripcion)
select a;
return new List<ITieneID>(resultado.Cast<ITieneID>());
}
of a little more concise:
public override List<ITieneID> Buscar(ITieneID elementoPatron)
{
return new List<ITieneID>(base.Repository.Context.Areas
.Where(a=>a.areaID == elementoPatron.ID || a.areaDescripcion.Contains(elementoPatron.Descripcion))
.Cast<ITieneID>());
}

Well, if you need to do this in two "hops" in order to work around any LINQ to SQL limitations, you could use LINQ to SQL and then LINQ to Objects:
var resultado = from a in base.Repository.Context.Areas
where a.areaID.Equals(elementoPatron.ID) ||
a.areaDescripcion.Contains(elementoPatron.Descripcion)
select new AtlasWFM_Entities.Clases.Area
{
ID = a.areaID,
Descripcion = a.areaDescripcion,
Estado = a.areaEstado,
};
return resultado.AsEnumerable() // Do the rest in LINQ to objects
.Select(r => new AtlasWFM_Entities.Clases.Area
{
ID = r.ID,
Descripcion = r.Descripcion,
Estado = r.Estado,
})
.ToList();

If Area implements the ITieneID interface just do
var resultado = from a in base.Repository.Context.Areas
where a.areaID.Equals(elementoPatron.ID) || a.areaDescripcion.Contains(elementoPatron.Descripcion)
select new AtlasWFM_Entities.Clases.Area
{
ID = a.areaID,
Descripcion = a.areaDescripcion,
Estado = a.areaEstado,
};
result = resultado.ToList().ConvertAll(x=> x as ITieneID);
return result;

Related

How do I fill a child list inside a parent list using linq2db in a single query in my .net core app?

I am trying to query my database to return turn reports with any attachments included. I need a list of turn report items which are returned by date, and then for each report I want it to also return all of the attachments associated with the turn reports. The only way to associate them is by the EntryId.
Here is my method to get the turn reports:
public List<TurnReportItem> GetTurnReportsByDateShiftAndDept(DateTime shiftStart, int shiftNum, int dept)
{
try
{
List<TurnReportItem> list;
using (connection)
{
list = (from r in connection.VTurnReports
join a in connection.TurnReportAreas on r.AreaId equals a.AreaId
where a.DeptId == dept && a.Enabled && r.ShiftDate == shiftStart && r.ShiftNum == shiftNum
select new TurnReportItem
{
areaId = r.AreaId,
areaName = a.Name,
author = r.Author,
comment = r.Comment,
datetime = r.Datetime,
id = r.EntryId,
ip = r.Ip,
shiftDate = r.ShiftDate,
shiftNum = r.ShiftNum,
sort_order = a.SortOrder,
attachment_count = r.AttachmentCount,
attachments = (
from at in connection.TurnReportAttachments where at.EntryId == r.EntryId
select new TurnReportAttachment
{
AttachmentId = at.AttachmentId,
FileName = at.FileName
}).ToList()
})
.OrderBy(r => r.sort_order)
.OrderBy(r => r.datetime)
.ToList();
return list;
}
}
Here is the TurnReportItem class that I am filling. If I do not have the subquery I do get all of the turnreports.
public class TurnReportItem
{
public int id;
public string comment;
public DateTime datetime;
public string author;
public int areaId;
public string areaName;
public DateTime shiftDate;
public int shiftNum;
public string ip;
public int? attachment_count;
public int sort_order;
public int area_rating;
public List<TurnReportAttachment> attachments;
public TurnReportItem() { }
}
I have a separate method that will return the all of the comments with the entry id. I have tried to fill the list using that method. I am converting this from a MVC app and I was able to use the method to fill the list however it will not work when I try it in this app, I also would prefer to only make one connection in the database to get what I need.
List<TurnReportItem> list;
using (connection)
{
list = (from r in connection.VTurnReports
join a in connection.TurnReportAreas on r.AreaId equals a.AreaId
where a.DeptId == dept && a.Enabled && r.ShiftDate == shiftStart && r.ShiftNum == shiftNum
select new TurnReportItem
{
areaId = r.AreaId,
areaName = a.Name,
author = r.Author,
comment = r.Comment,
datetime = r.Datetime,
id = r.EntryId,
ip = r.Ip,
shiftDate = r.ShiftDate,
shiftNum = r.ShiftNum,
sort_order = a.SortOrder,
attachment_count = r.AttachmentCount,
attachments = SelectAttachmentsByEntryId(r.EntryId)
})
.OrderBy(r => r.sort_order)
.OrderBy(r => r.datetime)
.ToList();
return list;
}
public List<TurnReportAttachment> SelectAttachmentsByEntryId(int EntryId)
{
using (connection)
{
// we do it this way so that we don't return the blob
var results = from p in connection.TurnReportAttachments
where p.EntryId == EntryId
select new TurnReportAttachment
{
EntryId = p.EntryId,
AttachmentId = p.AttachmentId,
FileName = p.FileName
};
return results.ToList();
}
}
In your case SelectAttachmentsByEntryId should be static, with additional parameter connection. To make it work, it is needed to use ExpressionMethod.
public static class ReportHelpers
{
[ExpressionMethod(nameof(SelectAttachmentsByEntryIdImpl))]
public static List<TurnReportAttachment> SelectAttachmentsByEntryId(MyConnection connection, int EntryId)
{
throw new InvalidOperationException(); // should never enter here
}
private static Expression<Func<MyConnection, int, List<TurnReportAttachment>>> SelectAttachmentsByEntryIdImpl()
{
return (connection, EntryId) =>
(from p in connection.TurnReportAttachments
where p.EntryId == EntryId
select new TurnReportAttachment
{
EntryId = p.EntryId,
AttachmentId = p.AttachmentId,
FileName = p.FileName
})
.ToList();
}
}
Then you can use this method in queries:
public List<TurnReportItem> GetTurnReportsByDateShiftAndDept(DateTime shiftStart, int shiftNum, int dept)
{
using (connection)
{
var list = (from r in connection.VTurnReports
join a in connection.TurnReportAreas on r.AreaId equals a.AreaId
where a.DeptId == dept && a.Enabled && r.ShiftDate == shiftStart && r.ShiftNum == shiftNum
select new TurnReportItem
{
areaId = r.AreaId,
areaName = a.Name,
author = r.Author,
comment = r.Comment,
datetime = r.Datetime,
id = r.EntryId,
ip = r.Ip,
shiftDate = r.ShiftDate,
shiftNum = r.ShiftNum,
sort_order = a.SortOrder,
attachment_count = r.AttachmentCount,
attachments = ReportHelpers.SelectAttachmentsByEntryId(connection, r.EntryId)
})
.OrderBy(r => r.sort_order)
.ThenBy(r => r.datetime)
.ToList();
return list;
}
}
Note that OrderBy.OrderBy has no sense. It should be OrderBy.ThenBy

Unable to Convert Linq to Lambda Notation

public ActionResult EditArticle(int id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var typeId = 0;
var catId = 0;
var subCatId = 0;
var viewModel = (from sa in ems.SupportArticles
join ssc in ems.SupportSubCategories on sa.SubCatID equals ssc.SubCatID
join sc in ems.SupportCategories on ssc.CatID equals sc.CatID
join st in ems.SupportTypes on sc.TypeID equals st.TypeID
where sa.ArcticleId == id
select new SupportArticleViewModel { supportArticle = sa, supportSubCat = ssc, supportCat = sc, supportType = st });
foreach (var vm in viewModel)
{
typeId = vm.supportType.TypeID;
catId = vm.supportCat.CatID;
subCatId = vm.supportSubCat.SubCatID;
}
I want to convert it into Lambda Notation.But, I am unable to do it.Please help.I am using SupportViewModel which contains property of SupportType,SupportCategory ,SupportSubCategoryand SupportArticle.
Following is functional way do query , you have to make use of join function and than you get data
var filteredArtciles = SupportArticles.Where(sa=> sa.ArcticleId == id);
var query =
SupportArticles.
Join(SupportSubCategories,sa => sa.SubCatID ,ssc => ssc.SubCatID,(sa, ssc) => new {sa,ssc}).
Join(SupportCategories,sassc => sassc.ssc.CatID ,sc=>sc.CatID ,(sassc, sc) => new {sassc,sc}).;
Join(SupportTypes,sasscsc => sasscsc.sc.TypeID ,st=>st.TypeID ,(sc, st) => new {sasscsc,st}).
Select(j=>
new SupportArticleViewModel
{
supportArticle = j.sasscsc.sassc.sa,
supportSubCat = j.sasscsc.sassc.ssc,
supportCat = j.sasscsc.sc,
supportType = j.st
}
));

Not Returning properly

I was trying to get employee list which not already available in another list. but im getting only first element from array list.
ex : if i try EmployeeId = new int[2] {5, 2}; the list excluding only '5'. So please help me correct my below code.
public JsonResult GetEmployees(int[] EmployeeId)
{
var dbs = new dbContext();
if (EmployeeId != null)
{
foreach (var emp in EmployeeId)
{
var EmpList = dbs.Employees.Select(e => new
{
EmployeeId = e.EmployeeId,
Name = e.EmployeeName,
Job = e.Job.JobName,
Currency = e.Currency.CurrencyName,
Amount = e.Amount
}).Where(o => o.EmployeeId != emp);
return Json(EmpList, JsonRequestBehavior.AllowGet);
}
}
return null
}
Try this :
var employeeList = dbs.Employees.
.Where(e => EmployeeId.All(x=> x != e.EmployeeId))
.Select(e => new
{
EmployeeId = e.EmployeeId,
Name = e.EmployeeName,
Job = e.Job.JobName,
Currency = e.Currency.CurrencyName,
Amount = e.Amount
});
return Json(EmpList, JsonRequestBehavior.AllowGet);
}
Have you tried stepping through your code?
Your foreach iterates over your EmployeeId array.
Since you have a return statement in your foreach it exits the function at that point and it only uses the first element of your array.
You need something like this:
public JsonResult GetEmployees(int[] EmployeeId)
{
var dbs = new dbContext();
if (EmployeeId != null)
{
var EmpList = dbs.Employees.Where(EmployeeId.Contains(e.EmployeeId))
.Select(e => new
{
EmployeeId = e.EmployeeId,
Name = e.EmployeeName,
Job = e.Job.JobName,
Currency = e.Currency.CurrencyName,
Amount = e.Amount
}).Where(o => o.EmployeeId != emp);
return Json(EmpList, JsonRequestBehavior.AllowGet);
}
return null;
}

Cannot be constructed in a LINQ to Entities query. Error

public ActionResult CreateApp(Guid id)
{
SMICParkingLotApplicationEntities1 dbb = new SMICParkingLotApplicationEntities1();
ApplicationDATA applicationData = (from a in dbb.ApplicationDATAs
where a.ApplicationID == id
select new ApplicationDATA
{
ApplicationID = a.ApplicationID,
BrandModel = a.BrandModel,
CrNo = a.CrNo,
OrNo = a.OrNo,
DatePosted = a.DatePosted,
PoR = a.PoR,
PlateNo = a.PlateNo,
VehicleType = a.VehicleType
}).FirstOrDefault();
ApplicationSlotViewModel applicationSlotViewModel = new ApplicationSlotViewModel
{
ApplicationDatas = applicationData,
Application = new Application()
};
return View(applicationSlotViewModel);
Dunno what to do it always shows this error Cannot be constructed in a LINQ to Entities query. Error Help Plss..
If the type of ApplicationDatas in your ViewModel is ApplicationDATA, you can set it directly with the result of your query:
var applicationData =dbb.ApplicationDATAs.FirstOrDefault(a=>a.ApplicationID == id);
ApplicationSlotViewModel applicationSlotViewModel = new ApplicationSlotViewModel
{
ApplicationDatas = applicationData,
Application = new Application()
};
You can't project the result of your query using an existing entity type. Check the post that I quote in my comment
Remove className after new keyword. Try below code.
var applicationData = (from a in dbb.ApplicationDATAs
where a.ApplicationID == id
select new
{
ApplicationID = a.ApplicationID,
BrandModel = a.BrandModel,
CrNo = a.CrNo,
OrNo = a.OrNo,
DatePosted = a.DatePosted,
PoR = a.PoR,
PlateNo = a.PlateNo,
VehicleType = a.VehicleType
}).FirstOrDefault();

Refactoring C# code - doing more within Linq

The code below is what I currently have and works fine. I feel that I could do more of the work I am doing in Linq instead of C# code.
Is there is anyone out there who can accomplish the same result with more Linq code and less C# code.
public List<Model.Question> GetSurveyQuestions(string type, int typeID)
{
using (eMTADataContext db = DataContextFactory.CreateContext())
{
List<Model.Question> questions = new List<Model.Question>();
List<Linq.Survey_Question> survey_questions;
List<Linq.Survey> surveys = db.Surveys
.Where(s => s.Type.Equals(type) && s.Type_ID.Equals(typeID))
.ToList();
if (surveys.Count > 0)
{
survey_questions = db.Survey_Questions
.Where(sq => sq.Survey_ID == surveys[0].ID).ToList();
foreach (Linq.Survey_Question sq in survey_questions)
{
Model.Question q = Mapper.ToBusinessObject(sq.Question);
q.Status = sq.Status;
questions.Add(q);
}
}
else
{
questions = null;
}
return questions;
}
}
Here is my Mapper function from my Entity to Biz Object
internal static Model.Question ToBusinessObject(Linq.Question q)
{
return new Model.Question
{
ID = q.ID,
Name = q.Name,
Text = q.Text,
Choices = ToBusinessObject(q.Question_Choices.ToList())
};
}
I want my mapper funciton to map the Question Status like so.
internal static Model.Question ToBusinessObject(Linq.Question q)
{
return new Model.Question
{
ID = q.ID,
Name = q.Name,
Text = q.Text,
Choices = ToBusinessObject(q.Question_Choices.ToList()),
Status = q.Survey_Questions[?].Status
};
}
? the issue is this function does not know which survey to pull the status from.
Instead of creating the biz object then setting the Status property in a foreach loop like so
foreach (Linq.Survey_Question sq in survey_questions)
{
Model.Question q = Mapper.ToBusinessObject(sq.Question);
q.Status = sq.Status;
questions.Add(q);
}
I would like to somehow filter the EntitySet<Survey_Question> in the q object above in the calling method, such that there would only be one item in the q.Survey_Questions[?] collection.
below is my database schema and business object schema
What I needed to do was setup a join.
public List<Model.Question> GetSurveyQuestions(string type, int typeID)
{
using (eMTADataContext db = DataContextFactory.CreateContext())
{
return db.Survey_Questions
.Where(s => s.Survey.Type.Equals(type) && s.Survey.Type_ID.Equals(typeID))
.Join(db.Questions,
sq => sq.Question_ID,
q => q.ID,
(sq, q) => Mapper.ToBusinessObject(q, sq.Status)).ToList();
}
}
And then overload my Mapper Function
internal static Model.Question ToBusinessObject(Linq.Question q, string status)
{
return new Model.Question
{
ID = q.ID,
Name = q.Name,
Text = q.Text,
Status = status,
Choices = ToBusinessObject(q.Question_Choices.ToList()),
};
}
from question in db.Survey_Questions
let surveys = (from s in db.Surveys
where string.Equals(s.Type, type, StringComparison.InvariantCultureIgnoreCase) &&
s.Type_ID == typeID)
where surveys.Any() &&
surveys.Contains(s => s.ID == question.ID)
select new Mapper.Question
{
ID = question.Id,
Name = question.Name,
Text = question.Text,
Choices = ToBusinessObject(question.Question_Choices.ToList()),
Status = question.Status
}
Does that get you on the right track?
Why are you duplicating all your classes? You could just extend the LINQ to SQL classes with your business logic - they are partial classes. This is somewhat against the purpose of an OR mapper - persisting business entities.

Categories

Resources