Dynamic List in Entity FrameWork WebApi 2.0 - c#

Im using DTO's in EntityFrameWork with WebApi 2.0 , so I want to retrieve all the Orders, within the Orders, OrderProducts is a list in my program, I want to retrieve all the OrderProducts related to that Order my code right now is the following:
public async Task < IHttpActionResult > GetOrder() {
var order = from x in db.Order
select new OrderDTO {
OrderId = x.OrderId,
UserId = x.UserId,
orderStatusCode = x.orderStatusCode,
OrderProducts = new List < OrderProductDTO > {
new OrderProductDTO {
OrderId = x.OrderProducts.Select(y = >y.OrderId).FirstOrDefault(),
OrderProductId = x.OrderProducts.Select(y = >y.OrderProductId).FirstOrDefault(),
ProductId = x.OrderProducts.Select(y = >y.ProductId).FirstOrDefault(),
Product = new ProductDTO() {
productDesc = x.OrderProducts.Select(y = >y.Product.productDesc).FirstOrDefault(),
ProductId = x.OrderProducts.Select(y = >y.Product.ProductId).FirstOrDefault(),
productName = x.OrderProducts.Select(y = >y.Product.productName).FirstOrDefault(),
productPrice = x.OrderProducts.Select(y = >y.Product.productPrice).FirstOrDefault(),
}
}
},
purchaseDate = x.purchaseDate,
quantityOrder = x.quantityOrder,
totalOrderPrice = x.totalOrderPrice,
User = new UserDTO {
UserId = x.UserId,
username = x.User.username,
userInfo = new UserInfoDTO {
adress = x.User.UserInfo.adress,
city = x.User.UserInfo.city,
country = x.User.UserInfo.country,
zip = x.User.UserInfo.zip
}
}
};
return Ok(order);
Everything appears to be ok, but when I call the WebApi only the first element is returned, not all the elements in OrderProduct:
Any idea how to retrieve all the OrderProducts? Thanks.

Well you're only populating a single item in your query. Instead, you should do this:
....
OrderProducts = x.OrderProducts.Select(op => new OrderProductDTO
{
OrderId = op.OrderId,
OrderProductId = op.OrderProductId,
//etc
}
....

It looks like you're only asking for one Product instead of a List
Product = new ProductDTO() {
productDesc = x.OrderProducts.Select(y = >y.Product.productDesc).FirstOrDefault(),
ProductId = x.OrderProducts.Select(y = >y.Product.ProductId).FirstOrDefault(),
productName = x.OrderProducts.Select(y = >y.Product.productName).FirstOrDefault(),
productPrice = x.OrderProducts.Select(y = >y.Product.productPrice).FirstOrDefault(),
}
Should probably be
Products = new List<ProductDTO>() {...}

Related

How to update AmountDue value by InvoiceId in XERO API using C#

I am getting the list of Invoice details by Invoice Id.
Now i want to update AmountDue by respective Invoice Id.
i tried by below code:
ByInvoiceId.AmountDue = Convert.ToDecimal(100.00);
public_app_api.Invoices.Update(ByInvoiceId);
but..Error as
"A validation exception occurred"
What's the reason behind and How to solve this?
There are a number of ways to change the amount due on an invoice, however changing the value of the property directly is not one of them.
The amount due on an invoice is driven by the totals of the line items on the invoice minus the total of payments and allocated credit. One way to change the amount due is to change the values of your line items or add/remove some line items. You won't be able to change the invoice if it has been paid/partially paid.
Another way you could change the amount due on an invoice is to add a payment against the invoice or allocate credit from a credit note, prepayment, or overpayment to the invoice
In addition to MJMortimer's answer.
You can't change the line amounts on an AUTHORISED invoice via the c# API. You have to VOID the invoice and create a new one. You can however update DRAFT and SUBMITTED ones by updating the line items.
EDIT: Here is some code to help you. This is create invoice code, but amending one is essentially the same.
public XeroTransferResult CreateInvoices(IEnumerable<InvoiceModel> invoices, string user, string status)
{
_user = XeroApiHelper.User(user);
var invoicestatus = InvoiceStatus.Draft;
if (status == "SUBMITTED")
{
invoicestatus = InvoiceStatus.Submitted;
}
else if (status == "AUTHORISED")
{
invoicestatus = InvoiceStatus.Authorised;
}
var api = XeroApiHelper.CoreApi();
var xinvs = new List<Invoice>();
foreach (var inv in invoices)
{
var items = new List<LineItem>();
foreach (var line in inv.Lines)
{
decimal discount = 0;
if (line.PriceBeforeDiscount != line.Price)
{
discount = (decimal)(1 - line.Price / line.PriceBeforeDiscount) * 100;
}
items.Add(new LineItem
{
AccountCode = line.AccountCode,
Description = line.PublicationName != "N/A" ? line.PublicationName + " - " + line.Description : line.Description,
TaxAmount = (decimal)line.TaxAmount,
Quantity = 1,
UnitAmount = (decimal)line.PriceBeforeDiscount,
DiscountRate = discount,
TaxType = line.XeroCode,
ItemCode = line.ItemCode
});
}
var person = inv.Company.People.FirstOrDefault(p => p.IsAccountContact);
if (person == null)
{
person = inv.Company.People.FirstOrDefault(p => p.IsPrimaryContact);
}
var ninv = new Invoice
{
Number = inv.ClientInvoiceId,
Type = InvoiceType.AccountsReceivable,
Status = invoicestatus,
Reference = inv.Reference,
Contact = new Contact
{
Name = inv.Company.OrganisationName,
//ContactNumber = "MM" + inv.Company.CompanyId.ToString(),
FirstName = person.FirstName,
LastName = person.LastName,
EmailAddress = person.Email,
Phones = new List<Phone>()
{
new Phone {PhoneNumber = person.Telephone, PhoneType = PhoneType.Default},
new Phone {PhoneNumber = person.Mobile, PhoneType = PhoneType.Mobile}
},
Addresses = new List<Address>
{ new Address
{
//AttentionTo = inv.Company.People.FirstOrDefault(p => p.IsAccountContact) == null
//? inv.Company.People.FirstOrDefault(p=> p.IsPrimaryContact).FullName
//: inv.Company.People.FirstOrDefault(p => p.IsAccountContact).FullName,
//AddressLine1 = inv.Company.OrganisationName,
AddressLine1 = inv.Company.Address.Address1,
AddressLine2 = inv.Company.Address.Address2 ?? "",
AddressLine3 = inv.Company.Address.Address3 ?? "",
Region = inv.Company.Address.CountyState,
City = inv.Company.Address.TownCity,
PostalCode = inv.Company.Address.PostCode,
}
}
},
AmountDue = (decimal)inv.TotalAmount,
Date = inv.InvoiceDate,
DueDate = inv.DueDate,
LineItems = items,
LineAmountTypes = LineAmountType.Exclusive
};
if (SessionContext.TransferContactDetailsToXero == false)
{
ninv.Contact = new Contact
{
Id = inv.Company.XeroId ?? Guid.Empty,
Name = inv.Company.OrganisationName
};
}
xinvs.Add(ninv);
}
var success = true;
var xinvresult = new List<Invoice>();
try
{
api.SummarizeErrors(false);
xinvresult = api.Invoices.Create(xinvs).ToList();
}
catch (ValidationException ex)
{
// Something's gone wrong
}
foreach (var inv in xinvresult)
{
var mminvoice = invoices.FirstOrDefault(i => i.ClientInvoiceId == inv.Number);
if (inv.Errors != null && inv.Errors.Count > 0)
{
success = false;
if (mminvoice != null)
{
var errors = new List<XeroError>();
foreach (var err in inv.Errors)
{
errors.Add(new XeroError { ErrorDescription = err.Message });
}
mminvoice.XeroErrors = errors;
}
}
else
{
mminvoice.XeroTransferDate = DateTime.Now;
mminvoice.XeroId = inv.Id;
mminvoice.XeroErrors = new List<XeroError>();
}
}
return new XeroTransferResult
{
Invoices = invoices,
Success = success
};
}

How to define in the lambda expression as another function in LINQ Select Method

I am creating Azure Mobile app.
In the api I created following list with Entity Framework 6.1.
The problem is that it is too big in the Select methods,
public IHttpActionResult GetAllQABundle(string qAUser_id, int offset = 0, int limit = 20)
{
List<QABundleWithCommentDto> qABundleWithCommentDtos = context.QABundles
.Where(b => b.TeamId == null)
.OrderByDescending(b => b.UpdatedAt)
.Skip(offset)
.Take(limit)
.Select(b => new QABundleWithCommentDto()
{
Id = b.Id,
TagForFreeFormat = b.TagForFreeFormat,
MovieUrl = b.Movie.MovieUrl,
MovieThumbnailUrl = b.Movie.MovieThumbnailUrl,
MovieStreamUrl = b.Movie.MovieStreamUrl,
NumberOfViews = b.Movie.NumberOfViews,
Title = b.Title,
Description = b.Description,
QuestionType = b.QuestionType,
WannaKnowId = b.WannaKnows.Where(w => w.QAUserId == qAUser_id).FirstOrDefault().Id,
WannaKnowCount = b.WannaKnows.Count,
QAUserThumbnailUrl = b.QAUser.ThumbnailUrl,
QAUserId = b.QAUser.Id,
UserName = b.QAUser.UserName,
UpdatedAt = b.UpdatedAt,
GenereDtos = b.Generes.Select(t => new GenereDto() { GenereId = t.Id, GenereName = t.Name }).ToList(),
CommentDtos = b.Comments.OrderByDescending(c => c.UpdatedAt).Take(20).Select(c => new CommentDto()
{
Id = c.Id,
Text = c.Text,
MovieUrl = c.Movie.MovieUrl,
MovieThumbnailUrl = c.Movie.MovieThumbnailUrl,
QAUserId = c.QAUserId,
UserName = c.QAUser.UserName,
QAUserBelongsTo = c.QAUser.BelongsTo,
QAUserThumbnailUrl = c.QAUser.ThumbnailUrl,
CommentCreatedAt = c.CreatedAt,
Likes = c.Likes.Count,
LikeId = c.Likes.Where(x => x.QAUserId == qAUser_id).FirstOrDefault().Id,
UpdatedAt = c.UpdatedAt,
QABundleId = c.QABundleId
}).ToList(),
TeamDto = new TeamDto()
{
Id = b.TeamId,
Name = b.Team.Name,
FollowedDtos = b.Team.QAUsers.Select(u => new FollowedDto()
{
Id = u.Id,
UserName = u.UserName,
ThumbnailUrl = u.ThumbnailUrl,
}).ToList()
},
BestAnswerDto = new BestAnswerDto() { Id = b.BestAnswerId, CommentId = b.BestAnswer.CommentId }
})
.ToList();
return Json(qABundleWithCommentDtos);
}
I would like to define following part as another function, but I do not know how I implement that.
Does anyone have good idea?
b => new QABundleWithCommentDto()
{
Id = b.Id,
TagForFreeFormat = b.TagForFreeFormat,
MovieUrl = b.Movie.MovieUrl,
MovieThumbnailUrl = b.Movie.MovieThumbnailUrl,
MovieStreamUrl = b.Movie.MovieStreamUrl,
NumberOfViews = b.Movie.NumberOfViews,
Title = b.Title,
Description = b.Description,
QuestionType = b.QuestionType,
WannaKnowId = b.WannaKnows.Where(w => w.QAUserId == qAUser_id).FirstOrDefault().Id,
WannaKnowCount = b.WannaKnows.Count,
QAUserThumbnailUrl = b.QAUser.ThumbnailUrl,
QAUserId = b.QAUser.Id,
UserName = b.QAUser.UserName,
UpdatedAt = b.UpdatedAt,
GenereDtos = b.Generes.Select(t => new GenereDto() { GenereId = t.Id, GenereName = t.Name }).ToList(),
CommentDtos = b.Comments.OrderByDescending(c => c.UpdatedAt).Take(20).Select(c => new CommentDto()
{
Id = c.Id,
Text = c.Text,
MovieUrl = c.Movie.MovieUrl,
MovieThumbnailUrl = c.Movie.MovieThumbnailUrl,
QAUserId = c.QAUserId,
UserName = c.QAUser.UserName,
QAUserBelongsTo = c.QAUser.BelongsTo,
QAUserThumbnailUrl = c.QAUser.ThumbnailUrl,
CommentCreatedAt = c.CreatedAt,
Likes = c.Likes.Count,
LikeId = c.Likes.Where(x => x.QAUserId == qAUser_id).FirstOrDefault().Id,
UpdatedAt = c.UpdatedAt,
QABundleId = c.QABundleId
}).ToList(),
TeamDto = new TeamDto()
{
Id = b.TeamId,
Name = b.Team.Name,
FollowedDtos = b.Team.QAUsers.Select(u => new FollowedDto()
{
Id = u.Id,
UserName = u.UserName,
ThumbnailUrl = u.ThumbnailUrl,
}).ToList()
},
BestAnswerDto = new BestAnswerDto() { Id = b.BestAnswerId, CommentId = b.BestAnswer.CommentId }
Its type is Expression<Func<QABudle, QABundleWithCommentDto>> so if you write:
Expression<Func<QABudle, QABundleWithCommentDto>> selector = b => new
{
//rest of the code
}
now you can use selector like this: .Select(selector)

How do i get data by calling a method in Entity Framework

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.

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS

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();

IQueryable, converting Anonymous Type to Strong Type

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();

Categories