private void LoadSubforums(Forum forum, XElement subforumsXML)
{
forum.Subforums = (from forumItem in subforumsXML.Descendants("forum")
select new Subforum()
{
ID = (string)forumItem.Element("id"),
Name = (string)forumItem.Element("name"),
URL = (string)forumItem.Element("url"),
Description = (string)forumItem.Element("description"),
Type = (string)forumItem.Element("type"),
TopicCount = (string)forumItem.Element("topics"),
ReplyCount = (string)forumItem.Element("replies"),
LastPost = ParseLastPost(forumItem.Element("lastpost")),
IsRedirect = How can I access the LastPost attribute here? :S
}).ToList();
}
}
The IsRedirect attribute of the Subforum class is dependant of a value inside the LastPost attribute.
Is it possible for me to access this attribute from within the Linq query?
EDIT:
This is what I'm trying to do:
forum.Subforums = (from forumItem in subforumsXML.Descendants("forum")
let lastPost = ParseLastPost(forumItem.Element("lastpost"))
select new Subforum()
{
ID = (string)forumItem.Element("id"),
Name = (string)forumItem.Element("name"),
URL = (string)forumItem.Element("url"),
Description = (string)forumItem.Element("description"),
Type = (string)forumItem.Element("type"),
TopicCount = (string)forumItem.Element("topics"),
ReplyCount = (string)forumItem.Element("replies"),
LastPost = lastPost,
IsRedirect = if(lastPost.ID == null) ? true ; false
}).ToList();
But this code doesn't compile.
forum.Subforums = (from forumItem in subforumsXML.Descendants("forum")
let lastPost = ParseLastPost(forumItem.Element("lastpost"))
select new Subforum()
{
ID = (string)forumItem.Element("id"),
Name = (string)forumItem.Element("name"),
URL = (string)forumItem.Element("url"),
Description = (string)forumItem.Element("description"),
Type = (string)forumItem.Element("type"),
TopicCount = (string)forumItem.Element("topics"),
ReplyCount = (string)forumItem.Element("replies"),
LastPost = lastPost,
IsRedirect = !string.IsNullOrEmpty(lastPost)
}).ToList();
Related
I want to make a menu, and this is my code. I need recursive function.
var newTopMenu = new TopMenuVm
{
TopMenus = (from amp in allMasterPages
orderby amp.DisplayOrder
select new TopMenuVm
{
DisplayOrder = amp.DisplayOrder,
Url = string.IsNullOrEmpty(amp.Url) ? GenerateUrl(amp.Page1.OrderBy(x => x.DisplayOrder).FirstOrDefault()) : amp.Url,
PageId = amp.PageId,
PageTitle = amp.PageTitle,
TopMenus = (from submenu in amp.Page1
orderby amp.DisplayOrder
select new TopMenuVm
{
DisplayOrder = submenu.DisplayOrder,
PageTitle = submenu.PageTitle,
PageId = submenu.PageId,
Url = string.IsNullOrEmpty(submenu.Url) ? GenerateUrl(submenu) : submenu.Url,
}).ToList()//Here is go on infinite...,
}).ToList()
};
How can i do that with linq, please help...
Is something like this what you are looking for?
// allMasterPages is type List<MyPage> - change to whatever it really is
Func<List<MyPage>, List<TopMenuVM>> funcTopMenuVM = null;
funcTopMenuVM = ((pages) => {
return
(from amp in pages
orderby amp.DisplayOrder
select new TopMenuVM()
{
DisplayOrder = amp.DisplayOrder,
Url = string.IsNullOrEmpty(amp.Url) ? GenerateUrl(amp.Page1.OrderBy(x => x.DisplayOrder).FirstOrDefault()) : amp.Url,
PageId = amp.PageId,
PageTitle = amp.PageTitle,
TopMenus = funcTopMenuVM(amp.Page1)
}).ToList();
});
var newTopMenu = new TopMenuVM()
{
TopMenus = funcTopMenuVM(allMasterPages)
};
Of course, it doesn't have to be a Func<>, it could just be a regular method that returns a List<TopMenuVM> and takes a List<MyPage> (or whatever allMasterPages is).
I want to get ResourceNames on the base of Id in EntityFramework but it is giving error: "LINQ to Entities does not recognize the method 'System.String GetResourceNameById(Int32)' method, and this method cannot be translated into a store expression."
Following ismy code.
public string GetResourceNameById(int Id)
{
return _DBContex.Employees.Where(x => x.Id == Id).FirstOrDefault().FirstName;
}
public CygnusInternalResponseViewModel GetAllTimeEntriesForGrid(int start = 0, int perPage = -1, string sortColumn = "", string sortDirection = "")
{
List<TimeEntryViewModel> te = new List<TimeEntryViewModel>();
te = (from jb in _DBContex.TimeEntries
select new TimeEntryViewModel
{
Id = jb.Id,
ResourceId = (int)jb.ResourceId,
TicketId = (int)jb.TicketId,
WorkType = (WorkTypeCatalog)jb.WorkType,
HoursWorked = (float)jb.HoursWorked,
Status = (TimeEntryStatusCatalog)jb.Status,
Role = (RoleCatalog)jb.Role,
StartTime = (TimeSpan)jb.StartTime,
EndTime = (TimeSpan)jb.EndTime,
SummaryNotes = jb.SummaryNotes,
InternalNotes = jb.InternalNotes,
Contract = (DateTime)jb.Contract,
Date = (DateTime)jb.Date,
ResourceName = GetResourceNameById((int)jb.ResourceId) // ERRORR Line
}).ToList();
You might need to create in memory representation of your database table.
public CygnusInternalResponseViewModel GetAllTimeEntriesForGrid(int start = 0, int perPage = -1, string sortColumn = "", string sortDirection = "")
{
List<TimeEntryViewModel> te = new List<TimeEntryViewModel>();
var query=_DBContex.TimeEntries.ToList();//create in-memory representation
te = (from jb in query
select new TimeEntryViewModel
{
Id = jb.Id,
ResourceId = (int)jb.ResourceId,
TicketId = (int)jb.TicketId,
WorkType = (WorkTypeCatalog)jb.WorkType,
HoursWorked = (float)jb.HoursWorked,
Status = (TimeEntryStatusCatalog)jb.Status,
Role = (RoleCatalog)jb.Role,
StartTime = (TimeSpan)jb.StartTime,
EndTime = (TimeSpan)jb.EndTime,
SummaryNotes = jb.SummaryNotes,
InternalNotes = jb.InternalNotes,
Contract = (DateTime)jb.Contract,
Date = (DateTime)jb.Date,
ResourceName = GetResourceNameById((int)jb.ResourceId)
}).ToList();
te = (from jb in _DBContex.TimeEntries
select new TimeEntryViewModel
{
Id = jb.Id,
ResourceId = (int)jb.ResourceId,
TicketId = (int)jb.TicketId,
WorkType = (WorkTypeCatalog)jb.WorkType,
HoursWorked = (float)jb.HoursWorked,
Status = (TimeEntryStatusCatalog)jb.Status,
Role = (RoleCatalog)jb.Role,
StartTime = (TimeSpan)jb.StartTime,
EndTime = (TimeSpan)jb.EndTime,
SummaryNotes = jb.SummaryNotes,
InternalNotes = jb.InternalNotes,
Contract = (DateTime)jb.Contract,
Date = (DateTime)jb.Date
}).ToList();
te.Foreach(t=>t.ResourceName = GetResourceNameById((int)t.ResourceId);
First you get the records then you set the ResourceName
You can call ToList() method before your SELECT and then call your function.
what is the wrong with this LINQ statement:
(from deposit in DAOBase<CashBookDeposit, long>.GetIQueryableBusinessBase()
where deposit.Id == id
select new CashBookDeposit
{
Id = deposit.Id,
DepositeNumber = deposit.DepositeNumber,
DocumentTypeName = deposit.CashBookDepositDocumentType.EnglishName,
StatusName = deposit.CashBookDepositeStatus.EnglishName,
CashbookName = deposit.CashBook.EnglishName,
Collector = deposit.Collector,
Partner = deposit.Partner,
CashBookDepositDocumentType = deposit.CashBookDepositDocumentType,
CreationDate = deposit.CreationDate,
PaidAmount = deposit.TotalAmount,
Description = deposit.Description,
TempReceipts = (from receipt in deposit.TempReceipts
select new TempReceipt
{
Id = receipt.Id,
Code = receipt.Code,
ReceiptNo = receipt.ReceiptNo,
Amount = receipt.Amount,
BusinessPartnerName = receipt.Partner.ENName,
CollectorName = receipt.Collector.EnglishName,
StatusName = receipt.TempReceiptStatus.EnglishName,
CollectionDate = receipt.CollectionDate,
CreationDate = receipt.CreationDate,
Description = receipt.Description,
}).ToList()
}).SingleOrDefault();
i got this exception :
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
need help, thanks
You can change the query as follows:
(from deposit in DAOBase<CashBookDeposit, long>.GetIQueryableBusinessBase()
let receipts = (from tempReceipts in deposit.TempReceipts
select tempReceipts)
where deposit.Id == id
select new CashBookDeposit
{
Id = deposit.Id,
DepositeNumber = deposit.DepositeNumber,
DocumentTypeName = deposit.CashBookDepositDocumentType.EnglishName,
StatusName = deposit.CashBookDepositeStatus.EnglishName,
CashbookName = deposit.CashBook.EnglishName,
Collector = deposit.Collector,
Partner = deposit.Partner,
CashBookDepositDocumentType = deposit.CashBookDepositDocumentType,
CreationDate = deposit.CreationDate,
PaidAmount = deposit.TotalAmount,
Description = deposit.Description,
TempReceipts = (from receipt in receipts
select new TempReceipt
{
Id = receipt.Id,
Code = receipt.Code,
ReceiptNo = receipt.ReceiptNo,
Amount = receipt.Amount,
BusinessPartnerName = receipt.Partner.ENName,
CollectorName = receipt.Collector.EnglishName,
StatusName = receipt.TempReceiptStatus.EnglishName,
CollectionDate = receipt.CollectionDate,
CreationDate = receipt.CreationDate,
Description = receipt.Description,
}).ToList()
}).SingleOrDefault();
Is there a more elegant/concise way of this; I'd like to get rid of foreach loop with the WorkListItem initialization code.
var queryable = registrations.Select(
r => new
{
r.Id, r.AccountNumber, r.DateAdded, r.DateUpdated, r.Patient, r.Patient.InsuranceInfos
});
var list = queryable.ToList();
var workListItems = new List<WorkListItem>();
foreach (var anonymous in list)
{
var w = new WorkListItem
{
Id = anonymous.Id,
ClientAccountId = anonymous.AccountNumber,
DateAdded = anonymous.DateAdded,
DateUpdated = anonymous.DateUpdated,
Patient = anonymous.Patient,
InsuraceInfos = anonymous.Patient.InsuranceInfos
};
workListItems.Add(w);
}
return workListItems;
Yes you can completely cut out the "middle-man" as it were and select straight into a new WorkListItem as below:
var list = registrations.Select(r => new WorkListItem
{
Id = r.Id,
ClientAccountId = r.AccountNumber,
DateAdded = r.DateAdded,
DateUpdated = r.DateUpdated,
Patient = r.Patient,
InsuraceInfos = r.Patient.InsuranceInfos
}).ToList();
var setsA = new List<SetA> {
new SetA { SsnA = "3450734507", name = "setA"},
new SetA { SsnA = "6833467788", name = "setA"},
new SetA { SsnA = "5452347787", name = "setA"},
new SetA { SsnA = "9345345345", name = "setA"},
};
var setsB = new List<SetB> {
new SetB { SsnB = "5452347787" ,name = "setB"},
new SetB { SsnB = "9345345345", name = "setB"},
};
when i use this linq:
var Set =
from seta in setsA
join setb in setsB
on seta.SsnA
equals setb.SsnB
select new {
SSN = seta.SsnA,
NAME = setb.name
};
i get this value:
{ SSN = "5452347787", NAME = "setB" }
{ SSN = "9345345345", NAME = "setB" }
but i would want to have SET which combines these two and the result would be:
{ SSN = "3450734507", NAME = "setA" }
{ SSN = "6833467788", NAME = "setA" }
{ SSN = "5452347787", NAME = "setB" }
{ SSN = "9345345345", NAME = "setB" }
This would be a result set that would tell me with the name NAME property which set it was taken from, if SSN was found in SetA and SetB it would have property NAME = "setB"
could someone help me with this?
It seems you want an outer join - this is done using GroupJoin:
var set = setsA.GroupJoin(
setsB,
sa => sa.SsnA,
sb => sb.SsnB,
(a, bs) => new { SSN = a.SsnA, NAME = bs.Any() ? "setB" : "setA" });
The LINQ way described here:
http://msdn.microsoft.com/en-us/library/bb397895.aspx would look like this (functionally same to the lambda way):
var set = from a in setsA
join b in setsB on a.SsnA equals b.SsnB into g
from o in g.DefaultIfEmpty()
select new { SSN = a.SsnA, NAME = (o != null ? o.name : a.name)};