I've got a Linq query with 5 joins/tables, with predefined conditions.
Because I need the query multiple times I've created a function which returns the default LINQ query as an IQueryable.
public static IQueryable<MroomLinqModel> GetDefaultQuery(CustomerContext CustomerCtx)
{
var Mrooms = (from mr in CustomerCtx.Mrooms
join m in CustomerCtx.Moves on mr.MoveId equals m.MoveId
join mg in CustomerCtx.mgroup on m.MgroupId equals mg.MgroupId
join s in CustomerCtx.Status on m.StatusId equals s.StatusId
join rt in CustomerCtx.Roomtypes on mr.RoomtypeId equals rt.Key
join g in CustomerCtx.Guests on m.Mgroup.GuestId equals g.GuestId
where
Math.Abs(mg.Status) != (int)IResStatus.InComplete &&
s.Visible
select new MroomLinqModel
{
OpenDepositPayments = mg.DepositPayments.Any(dp => !dp.Paid),
RoomHidden = (mr.RoomId == null ? true : mr.Room.Hidden),
StatusVisible = s.Visible,
MroomId = mr.MroomId,
MoveId = m.MoveId,
MgroupId = mg.MgroupId,
StatusId = s.StatusId,
StatusFlags = s.Flags,
BackgroundColor = s.Background_Argb,
TextColor = s.Foreground_Argb,
PersonCount = m.Movegroups.Sum(m => m.PersonCount),
MoveCount = mg.Moves.Count(),
RoomId = mr.RoomId,
PMSMroomId = mr.PMS_Id,
PMSMoveId = m.PMS_Id,
PMSMgroupId = mg.MgroupId_Casablanca,
From = mr.From,
Until = mr.Until,
EditableState = m.EditableState,
MroomStatus = mr.Status,
RoomtypeUsage = mr.Roomtype.Usage,
BookingReference = mg.ReferenceNumber,
Guest = g
});
return Mrooms;
}
Now I would like to add some conditions afterwards like:
Query = GetDefaultQuery.Where(q => !q.RoomHidden && q.From <= dtLoadEnd && dtLoadStart <= q.Until);
Query = Query.Where(q => q.RoomtypeUsage == RoomtypeUsageType.Roomplan);
This works fine but takes a lot more time to execute as if I add all conditions directly to the first LINQ query.
How do I form the query to access the original tables and generate a fast query?
Since all your condition match the table where you are joining the other tables you can add the conditions before your multi join, I got 2 suggestions for you, either create another method that filters your conditions predefined or add to your method optional filters like so :
public static IQueryable<MroomLinqModel> GetDefaultQuery(CustomerContext CustomerCtx, bool? roomHidden, DateTime? dtLoadEnd
/* you can add more parameters but for demonstrations purposes i'm only describing this 2*/)
{
var query = CustomerCtx.Mrooms;
if(roomHidden.HasValue)
{
query = query.Where( q=>q.From == roomHidden.Value)
}
if(dtLoadEnd .HasValue)
{
query = query.Where( q=>q.RoomHidden <= dtLoadEnd.Value)
}
// you can add more conditions
var Mrooms = (from query
join m in CustomerCtx.Moves on mr.MoveId equals m.MoveId
join mg in CustomerCtx.mgroup on m.MgroupId equals mg.MgroupId
join s in CustomerCtx.Status on m.StatusId equals s.StatusId
join rt in CustomerCtx.Roomtypes on mr.RoomtypeId equals rt.Key
join g in CustomerCtx.Guests on m.Mgroup.GuestId equals g.GuestId
where
Math.Abs(mg.Status) != (int)IResStatus.InComplete &&
s.Visible
select new MroomLinqModel
{
OpenDepositPayments = mg.DepositPayments.Any(dp => !dp.Paid),
RoomHidden = (mr.RoomId == null ? true : mr.Room.Hidden),
StatusVisible = s.Visible,
MroomId = mr.MroomId,
MoveId = m.MoveId,
MgroupId = mg.MgroupId,
StatusId = s.StatusId,
StatusFlags = s.Flags,
BackgroundColor = s.Background_Argb,
TextColor = s.Foreground_Argb,
PersonCount = m.Movegroups.Sum(m => m.PersonCount),
MoveCount = mg.Moves.Count(),
RoomId = mr.RoomId,
PMSMroomId = mr.PMS_Id,
PMSMoveId = m.PMS_Id,
PMSMgroupId = mg.MgroupId_Casablanca,
From = mr.From,
Until = mr.Until,
EditableState = m.EditableState,
MroomStatus = mr.Status,
RoomtypeUsage = mr.Roomtype.Usage,
BookingReference = mg.ReferenceNumber,
Guest = g
});
return Mrooms;
}
expecting that CustomerCtx.Mrooms is a DbSet<Mroom> and that Mroom looks like :
public class Mroom {
public int MroomId {get; set;}
[ForeignKey("Move")]
public int MoveId {get; set;} //FK
public virtual Move Move {get; set;} //Navigation property
//configuration may be needed cf annotation
[ForeignKey("Status")]
public int StatusId {get; set;}
public virtual Status Status {get; set;}
//... Mgroup, Guest, ...
}
If it is not the case I suggest you yo refactor your code to use navigation properties.
Then you may use PredicateBuilder of linqkit as follow:
public static IQueryable<MroomLinqModel> GetDefaultQuery(CustomerContext CustomerCtx, Expression<Function<Mroom, bool>> q)
{
Expression<Function<Mroom, bool>> w = PredicateBuilder.New<Mroom>
(s => Math.Abs(s.Mgroup.Status) != (int)IResStatus.InComplete &&
s.Visible);
w = w.And(q);
return
CustomerCtx.Mrooms.
Where(w.Expand()).
Select( x => new MroomLinqModel
{
OpenDepositPayments = x.Mgroup.DepositPayments.Any(dp => !dp.Paid),
RoomHidden = (x.RoomId == null ? true : x.Room.Hidden),
StatusVisible = x.Status.Visible,
MroomId = x.MroomId,
MoveId = x.MoveId,
MgroupId = x.MgroupId,
StatusId = x.Status.StatusId,
StatusFlags = x.Status.Flags,
BackgroundColor = x.Status.Background_Argb,
TextColor = x.Status.Foreground_Argb,
PersonCount = x.Move.Movegroups.Sum(m => m.PersonCount),
MoveCount = x.Mgroup.Moves.Count(),
RoomId = x.RoomId,
PMSMroomId = x.PMS_Id,
PMSMoveId = x.Move.PMS_Id,
PMSMgroupId = x.Mgroup.MgroupId_Casablanca,
From = x.From,
Until = x.Until,
EditableState = x.Move.EditableState,
MroomStatus = x.Move.Status,
RoomtypeUsage = mr.Roomtype.Usage,
BookingReference = x.Mgroup.ReferenceNumber,
Guest = x.Guest
}
);
}
and use it as:
GetDefaultQuery(ctx, q => !q.RoomHidden && q.From <= dtLoadEnd && dtLoadStart <= q.Until);
and or
Expression<Function<Mroom, bool>> w = PredicateBuilder.New<Mroom>(q =>
!q.RoomHidden && q.From <= dtLoadEnd && dtLoadStart <= q.Until);
//some logic
w = w.And(q => q.RoomtypeUsage == RoomtypeUsageType.Roomplan);
GetDefaultQuery(ctx, w);
Related
My interface is returning a IQueryable. The model TemplatesAgent with a template and IEnumerable<Agent> object. In my DataService I am collecting all of the agents in the ListOfAgent. I am then selecting the agent that are within the templates using the AgentVersionKey. This is the error I am getting is it related to the IQueryable type?
ExceptionMessage": "Explicit construction of entity type 'OrderTemplateTool.Data.Agent' in query is not allowed.",
Interface
IQueryable<TemplatesAgent> GetTemplateAgentNameDiseaseName(string agent, string disease);
DataService
public IQueryable<TemplatesAgent> GetTemplateAgentNameDiseaseName(string agent, string disease)
{
//Common
var ListOfAgent = (from a in UnitOfWork.GetRepository<Agent>().Get(a => !a.IsDeleted && a.IsCurrentVersion)
select new Agent
{
VersionKey = a.VersionKey,
Name = a.Name,
RxNormId = a.RxNormId,
BrandNames = a.BrandNames
});
var TemplatesAgent = (from t in UnitOfWork.GetRepository<Template>().Get()
join r in UnitOfWork.GetRepository<Regimen>().Get() on t.Id equals r.TemplateId
join rp in UnitOfWork.GetRepository<RegimenPart>().Get() on r.Id equals rp.RegimenId
join re in UnitOfWork.GetRepository<RegimenEntry>().Get() on rp.Id equals re.RegimenPartId
where t.IsCurrentVersion
&& t.Status == 7
select new TemplatesAgent
{
Template = t,
Agent = ListOfAgent.Where(x => x.VersionKey == re.AgentVersionKey).ToList()
});
Model
public class TemplatesAgent
{
public Template Template { get; set; }
public IEnumerable<Agent> Agent { get; set; }
}
Controller
[HttpGet]
public IHttpActionResult TemplatesList(string key, string disease = null, string agent = null)
{
var result = TemplatesDataService.GetTemplateAgentNameDiseaseName(agent, disease)
.Where(
t =>
t.Template.IsCurrentVersion && t.Template.Status == (short)TemplateMode.Published)
.OrderBy(t => t.Template.TemplateIdMain)
.ThenBy(t => t.Template.TemplateIdNumeric)
.ThenBy(t => t.Template.TemplateIdAlt)
.ToList()
.Select(t => new
{
TemplateId = t.Template.TemplateId,
RegimenTitle = t.Template.Title,
CourseTitle = t.Template.GroupTitle,
GuidelineTitle = t.Template.GuideLineTitle,
DiseaseId = t.Template.ExternalDiseaseId,
DiseaseName = t.Template.DiseaseName
Agent = t.Agent(a => new
{
VersionKey = a.VersionKey,
Name = a.Name,
BrandNames = a.BrandNames,
RxNormId = a.RxNormId
}).ToList(),
})
.Distinct().ToList();
return Json(result);
}
It is telling you that you can't make a new Agent inside the query. Try this.
var agentRepo = UnitOfWork.GetRepository<Agent>().Get();
var ListOfAgent = (from a in agentRepo
where !a.IsDeleted && a.IsCurrentVersion
select a);
var TemplatesAgent = (from t in UnitOfWork.GetRepository<Template>().Get()
join r in UnitOfWork.GetRepository<Regimen>().Get() on t.Id equals r.TemplateId
join rp in UnitOfWork.GetRepository<RegimenPart>().Get() on r.Id equals rp.RegimenId
join re in UnitOfWork.GetRepository<RegimenEntry>().Get() on rp.Id equals re.RegimenPartId
where t.IsCurrentVersion
&& t.Status == 7
select new TemplatesAgent
{
Template = t,
Agent = ListOfAgent.Where(x => x.VersionKey == re.AgentVersionKey).ToList()
});
Controller
[HttpGet]
public IHttpActionResult TemplatesList(string key, string disease = null, string agent = null)
{
var result = TemplatesDataService.GetTemplateAgentNameDiseaseName(agent, disease)
.Where(
t =>
t.Template.IsCurrentVersion && t.Template.Status == (short)TemplateMode.Published)
.OrderBy(t => t.Template.TemplateIdMain)
.ThenBy(t => t.Template.TemplateIdNumeric)
.ThenBy(t => t.Template.TemplateIdAlt)
.Distinct().ToList();
return Json(result);
}
I have to include both the newest Note and Comment with view model that I am passing to my list view but I cannot figure out how to include it in the view model.
I tried to do include but after I type p it will not give me a list of my properties for MinimumProductInfo which ProductNotes is a property for it.
Here is the controller code I am trying:
public ActionResult MinimumProductInfoList()
{
var Model = _minimumProductInfo.GetAll();
var Notes = db.ProductNotes.Where(p => p.NoteTypeFlag == "p").OrderByDescending(p => p.NoteDate).First();
var Comments = db.ProductNotes.Where(p => p.NoteTypeFlag == "c").OrderByDescending(p => p.NoteDate).First();
var Model = db.MinimumProductInfo.Include(p => p)
var ViewModel = Model.Select(x => new ProductInfoWithNoteList { MinimumProductInfoID = x.MinimumProductInfoID, ItemCode = x.ItemCode, MinimumOnHandQuantity = x.MinimumOnHandQuantity, MaximumOHandQuantity = x.MaximumOHandQuantity, MinimumOrderQuantity = x.MinimumOrderQuantity, LeadTimeInWeeks = x.LeadTimeInWeeks });
return View(ViewModel);
}
Everything else is working except now I need to include the latest note and latest comment in to my viewmodel
This is what I have now with WithMetta's help:
public ActionResult MinimumProductInfoList()
{
var productInfoViewModelCollection =
from x in db.MinimumProductInfo
let pnote =
(from inner_pnote in db.ProductNotes
where x.MinimumProductInfoID == inner_pnote.MinimumProductInfoID
&& inner_pnote.NoteTypeFlag == "p"
orderby inner_pnote.NoteDate
select inner_pnote).FirstOrDefault()
let cnote =
(from inner_cnote in db.ProductNotes
where x.MinimumProductInfoID == inner_cnote.MinimumProductInfoID
&& inner_cnote.NoteTypeFlag == "c"
orderby inner_cnote.NoteDate
select inner_cnote).FirstOrDefault()
select new ProductInfoWithNoteList
{
MinimumProductInfoID = x.MinimumProductInfoID,
ItemCode = x.ItemCode,
MinimumOnHandQuantity = x.MinimumOnHandQuantity,
MaximumOHandQuantity = x.MaximumOHandQuantity,
MinimumOrderQuantity = x.MinimumOrderQuantity,
LeadTimeInWeeks = x.LeadTimeInWeeks,
Comment = cnote.ToString(),
PermanentNote = pnote.ToString()
};
return View(productInfoViewModelCollection);
}
Maybe something like this using LINQ.
var productInfoViewModelCollection =
from x in db.MinimumProductInfo
where x != null
let pnote =
(from inner_pnote in db.ProductNotes
where inner_pnote != null
&& x.MinimumProductInfoID == inner_pnote.MinimumProductInfoID
&& inner_pnote.NoteTypeFlag == "p"
orderby inner_pnote.NoteDate descending
select inner_pnote).FirstOrDefault()
let cnote =
(from inner_cnote db.ProductNotes
where inner_cnote != null
&& x.MinimumProductInfoID == inner_cnote.MinimumProductInfoID
&& inner_cnote.NoteTypeFlag == "c"
orderby inner_cnote.NoteDate descending
select inner_cnote).FirstOrDefault()
select new ProductInfoWithNoteList {
MinimumProductInfoID = x.MinimumProductInfoID,
ItemCode = x.ItemCode,
MinimumOnHandQuantity = x.MinimumOnHandQuantity,
MaximumOHandQuantity = x.MaximumOHandQuantity,
MinimumOrderQuantity = x.MinimumOrderQuantity,
LeadTimeInWeeks = x.LeadTimeInWeeks,
Comment = cnote,
PermanentNote = pnote
};
I'm returning anonymouse type from my webapi controller, one of the values I need to be computed by using function. When I'm trying to do this way getting error say "Several actions were found that match request".
Here is how I call GET:
// GET api/Grafik/5
public IHttpActionResult GetGrafik(int id)
{
xTourist t = db.xTourist.Find(id);
var beach = db.xAdres.Find(t.Hotel).Kod;
var result = from a in db.Grafik
join b in db.Excursions on a.Excursion equals b.Kod
join c in db.Dates on a.KodD equals c.KodD
join d in db.Staff on a.Guide equals d.Kod
where c.Date > t.ArrDate && c.Дата < t.DepDate
let pu = from x in db.xPickUp where x.KodP == beach && x.Excursion == b.Kod select x.PickUpTime
orderby c.Date
select new { kodg = a.Kodg, excursion = b.Name, guide = d.GuideName, data = c.Date, pricead = b.Price,
pricech = b.PriceChd, pax = t.Pax, child = t.Ch, paxleft = GetPax(a.Kodg), pickup = pu.FirstOrDefault()};
return Ok(result);
}
And here is the function returning needed value:
public int GetPax(int id)
{
//get pax quota
var pre = db.Grafik.Where(k => k.Kodg == id).Select(p => p.Quota).SingleOrDefault();
if (pre.HasValue && pre.Value > 0)
{
//Get taken pax
var p = (from a in db.Orders where a.Kodg == id & !(a.Cansel == true) select a.Child).Sum();
var c = (from a in db.Orders where a.Kodg == id & !(a.Cansel == true) select a.Pax).Sum();
if (p.HasValue & c.HasValue)
{
return pre.Value - (p.Value + c.Value);
}
else
{
return pre.Value;
}
}
else
{
return 0;
}
}
Web API is seeing two public methods with id as a parameter and it can't figure out which one to execute when you send your request. Looking at your code, there's no need for the helper method GetPax() to be public. Try changing it to private.
private int GetPax(int id) // ...
I found solution. Ok, this could be a bit strange way, but it's working.
public IHttpActionResult GetГрафик(int id)
{
xTourist t = db.xTourist.Find(id);
var beach = db.xAdres.Find(t.Hotel).Kod;
var result = from a in db.Grafik
join b in db.Excursions on a.Excursion equals b.Kod
join c in db.Dates on a.Kodd equals c.Kodd
join d in db.Staff on a.Guidename equals d.Kod
join e in db.Заказы on a.КодГ equals e.КодГ
where c.Дата > t.Датапр && c.Дата < t.Датаотл
let pu = from x in db.xPickUp where x.КодП == beach && x.Excursion == b.Kod select x.PickUpTime
orderby c.Дата
let pl = a.Пребукинг - (e.Child + e.Pax)
select new { kodg = a.КодГ, excursion = b.Название, guide = d.Name, data = c.Дата, pricead = b.Price,
pricech = b.PriceChd, pax = t.Колчел, child = t.Дети, paxleft = pl, pickup = pu.FirstOrDefault()};
return Ok(result);
}
I want to write query more efficient.
I do not want before the end of the query, the list of data to extract.
var UserTimeLineNews = (from l in _newsService.NewsQuery()
where l.UserId == UserId && l.IsActive == true
orderby l.CreateDate descending
select new UserTimeLine
{
EventDate = l.CreateDate,
CreateDate = l.CreateDate,
NewsId = l.NewsId,
TimeLineType = TimeLineType.CreateNews,
Title = l.Title,
Abstract = l.NewsAbstract,
CommentCount = l.CommentCount,
LikeCount = l.LikeCount,
ViewsCount = l.ViewsCount,
Storyteller = l.Storyteller
}).AsQueryable();//Take(NumberOfNews).ToList();
var UserTimeLineLikeNews = (from l in _likeNewsService.LikeNewsQueryable()
where l.UserId == UserId
orderby l.CreateDate descending
select new UserTimeLine
{
EventDate = l.CreateDate,
CreateDate = l.CreateDate,
NewsId = l.NewsId,
TimeLineType = TimeLineType.LikeNews,
Title = l.News.Title,
Abstract = l.News.NewsAbstract,
CommentCount = l.News.CommentCount,
LikeCount = l.News.LikeCount,
ViewsCount = l.News.ViewsCount,
Storyteller = l.News.Storyteller
}).AsQueryable();//Take(NumberOfNews).ToList();
var UserTimeLineComments = (from l in _commentService.CommentQueryable()
where l.UserId == UserId && l.IsActive == true
orderby l.CreateDate descending
select new UserTimeLine
{
EventDate = l.CreateDate,
CreateDate = l.CreateDate,
NewsId = l.NewsId,
TimeLineType = TimeLineType.Comment,
Title = l.News.Title,
Abstract = l.News.NewsAbstract,
CommentContent = l.Content,
CommentCount = l.News.CommentCount,
LikeCount = l.News.LikeCount,
ViewsCount = l.News.ViewsCount,
Storyteller = l.News.Storyteller
}).AsQueryable();//Take(NumberOfNews).ToList();
var item = (UserTimeLineNews
.Union(UserTimeLineLikeNews)
.Union(UserTimeLineComments))
.OrderByDescending(e => e.EventDate)
.Distinct()
.Take(NumberOfNews)
.ToList();
After running the following error appears
Error:
The type 'UserTimeLine' appears in two structurally incompatible initializations within a single LINQ to Entities query.
A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.
The first two queries don't initialize the CommentContent property. Add that to the initializer in the first two queries (or remove it in the last query) and the final query should work.
I want to, in the midst of a linq to sql query where clause, check against a public int. I am getting this error: Method 'Int32 isInDept(System.String)' has no supported translation to SQL.
Vaguely related classes (from a public static class called ad):
//get AD property
public static string GetProperty(this Principal principal, String property) {
DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
if (directoryEntry.Properties.Contains(property))
return directoryEntry.Properties[property].Value.ToString();
else
return String.Empty;
}
public static string GetDepartment(this Principal principal) {
return principal.GetProperty("department");
}
The Classes in question (from a different class):
public int isInDept(string department) {
PrincipalContext domain = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domain, GetUserId());
if (department == userPrincipal.GetDepartment()) {
return 3;
}
else { return 2; }
}
public intranetGS.viewArticle viewArticle(int id) {
string user = GetUserId();
var result = ( from a in n.articles
join s in n.sections on a.section equals s.section_id
join p in n.privacies on a.privacy equals p.privacy_id
let iid = isInDept(s.name)
where (a.active == true && a.article_id == id && a.privacy < iid) ||
(a.active == true && a.article_id == id && a.privacy == 3 && a.author == user)
select new intranetGS.viewArticle {
articleId = a.article_id,
title = a.title,
author = a.author,
html = a.html,
section = s.name,
privacy = p.name,
dateCreated = a.date_created,
dateModified = a.date_modified,
userCreated = a.user_created,
userModified = a.user_modified
}).First();
var nv = (from v in n.navs
join s in n.sections on v.section equals s.section_id
let iid = isInDept(s.name)
where (v.active == true && s.name == result.section && v.privacy < 3) ||
(v.active == true && s.name == result.section && v.privacy == iid && v.user_created == user)
select v.html);
StringBuilder sb = new StringBuilder();
foreach (var r in nv) {
sb.Append(nv);
}
result.articleNav = sb.ToString();
return result;
}
What am I doing wrong? If I can't do it this way, how is it suggested that it be done?
It is not possible to translate that function to SQL, one workaround for this is to make most of your query with linq to sql, and use Linq to Objects for the rest. It should be something like this:
var query = ( from a in n.articles
join s in n.sections on a.section equals s.section_id
join p in n.privacies on a.privacy equals p.privacy_id
where (a.active == true && a.article_id == id)
select new intranetGS.viewArticle {
articleId = a.article_id,
title = a.title,
author = a.author,
html = a.html,
section = s.name,
privacy = p.name,
privacyId = a.privacy,
dateCreated = a.date_created,
dateModified = a.date_modified,
userCreated = a.user_created,
userModified = a.user_modified
}).ToList();
And then filter the list:
var result = query.Where(a => (a.privacyId < isInDept(a.section)) ||
(a.privacyId == 3 && a.author == user)).First();
Then you can do the same for the second query.