I want to return the List of records with Id and name, where the name will be appended with the domain as name(domain) if the names are non-unique and it will again append with code if domain is also non-unique, such as name(domain)(code).
To be Clear
name - for unique names,
name(domain) - for duplicated names
name(domain)(code) - for duplicated codes.
Below are the sample data and expected data
**Sample Data**
var filter = new List<Record>()
{
new Record { Id = 011, Name = "OAKLAWN", Code = 123, Domain = "p1000"},
new Record { Id = 012, Name = "OAKLAWN", Code = 124 , Domain = "p2000"},
new Record { Id = 013, Name = "OAKLAWN", Code = 125 , Domain = "p3000"},
new Record { Id = 014, Name = "OAKLAWN", Code = 126 , Domain = "p4000"},
new Record { Id = 015, Name = "OAKLAWN", Code = 127 , Domain = "p5000"},
new Record { Id = 016, Name = "PEAKLAWN", Code = 111 , Domain = "p6000"},
new Record { Id = 017, Name = "PERKLAWN", Code = 124 , Domain = "p6000"},
new Record { Id = 018, Name = "QUCKLAWN", Code = 122 , Domain = "p6000"}
};
**expected return data**
{ Id = 011, Name = "OAKLAWN(p1000)"},
{ Id = 012, Name = "OAKLAWN(p2000)(124)"},
{ Id = 013, Name = "OAKLAWN(p3000)"},
{ Id = 014, Name = "OAKLAWN(p2000)(126)"},
{ Id = 015, Name = "OAKLAWN(p5000)"},
{ Id = 016, Name = "PEAKLAWN"},
{ Id = 017, Name = "PERKLAWN"},
{ Id = 018, Name = "QUCKLAWN"}
How can I achieve this using c# LINQ.
.NET fiddle: https://dotnetfiddle.net/2UdaxY
Thanks.
var records = filter.GroupBy(
r => r.Name,
(key, group) => group.Select(r => new
{
r.Id,
Name = group.Count() == 1
? key
: group.Count(x => x.Domain == r.Domain) == 1
? $"{key}({r.Domain})"
: $"{key}({r.Domain})({r.Code})"
})
)
.SelectMany(r => r)
.ToArray();
.NET fiddle: https://dotnetfiddle.net/tIPZOR
Related
I've managed to create a payment using the C# .NET SDK, however it keeps showing up as 'unapplied' when I check in QBO.
I am providing the Invoice ID and tried to follow their developer API documentation but I been at this so long now that maybe I am missing something?
The following code creates the payment but doesn't 'receive' the payment towards the invoice, is there something I missed or need to do in order for the two to be linked together?
Payment payment = new Payment
{
ProcessPayment = false,
CustomerRef = new ReferenceType { name = customer.DisplayName, Value = customer.Id },
CurrencyRef = new ReferenceType { type = "Currency", Value = "CAD" },
TotalAmt = amount,
TotalAmtSpecified = true
};
if (method == PaymentTypes.Cash)
{
var paymentType = paymentMethods.FirstOrDefault(o => o.Id == "1");
if (paymentType != null)
{
payment.PaymentMethodRef = new ReferenceType()
{name = paymentType.Name, Value = paymentType.Id};
}
}
if (method == PaymentTypes.DebitCard)
{
var paymentType = paymentMethods.FirstOrDefault(o => o.Id == "9");
if (paymentType != null)
{
payment.PaymentMethodRef = new ReferenceType()
{ name = paymentType.Name, Value = paymentType.Id };
}
}
if (method == PaymentTypes.CreditCard)
{
var paymentType = paymentMethods.FirstOrDefault(o => o.Id == "8");
if (paymentType != null)
{
payment.PaymentMethodRef = new ReferenceType()
{ name = paymentType.Name, Value = paymentType.Id };
}
}
List<LinkedTxn> linkedTxns = new List<LinkedTxn>
{
new LinkedTxn()
{
TxnId = invoice.Id,
TxnType = TxnTypeEnum.Invoice.ToString()
},
};
foreach (Line line in invoice.Line)
{
//line.Amount = amount;
//line.AmountSpecified = true;
line.Received = amount;
line.ReceivedSpecified = true;
line.DetailType = LineDetailTypeEnum.PaymentLineDetail;
line.DetailTypeSpecified = true;
line.LinkedTxn = linkedTxns.ToArray();
}
payment.DepositToAccountRef = new ReferenceType() { Value = "5" };
payment.Line = invoice.Line;
payment.PaymentRefNum = reference;
DataService dataService = new DataService(serviceContext);
dataService.Add<Payment>(payment);
This is not an answer. However there's too much to add to the comments. I'm hoping this will be a helpful starting point (if not I'll remove it later).
Firstly I'd suggest refactoring your code and parameterise your variables. Doing so you should be able to preform repeatable testing in isolation.
When you preform the dataService.Add<Payment>(payment), store the response object as it may offer clues on how the request was processed. Alternatively if this is suppressing error messages, you might want to try an use Postman to send HTTP requests. This may help determine what's parameters are missing/ incorrect.
Avoid creating objects that are partially assigned it makes it a lot easier to read 7 work out which properties need to be assigned.
Also if you have a look at Full update a payment section on https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/payment
the json payload has an additional Line with the property TxnType set to CreditMemo. I would assume you'll need to add something like ReceivePayment or CreditCardPayment?
To refactor your code consider:
// TODO - Set variables for testing purposes.
// This can be added as params to a method.
decimal amount = 100;
string reference = "";
string invoiceId = ""; // invoice.Id
string customerDisplayName = ""; //customer.DisplayName
string customerId = ""; //customer.Id
string paymentName = "Cash"; // paymentType.Name
string paymentId = "1"; // paymentType.Id
List<Line> lines = new List<Line>(); // invoice.Line
if(lines.Count() == 0)
{
// TODO: You might want to check there are lines?
throw new ArgumentException("No invoice.");
}
Line[] invoiceLines = lines.Select(m => new Line()
{
AnyIntuitObject = m.AnyIntuitObject,
Amount = m.Amount,
AmountSpecified = m.AmountSpecified,
CustomField = m.CustomField,
Id = m.Id,
LineNum = m.LineNum,
Description = m.Description,
DetailType = LineDetailTypeEnum.PaymentLineDetail, //m.DetailType,
DetailTypeSpecified = true, //m.DetailTypeSpecified,
LinkedTxn = new List<LinkedTxn>
{
new LinkedTxn()
{
TxnId = invoiceId,
TxnType = TxnTypeEnum.Invoice.ToString() // TODO: Should this be ReceivePayment?
},
}.ToArray(), //m.LinkedTxn,
LineEx = m.LineEx,
Received = amount, //m.Received,
ReceivedSpecified = true // m.ReceivedSpecified
}).ToArray();
Payment payment = new Payment
{
ProcessPayment = false,
CustomerRef = new ReferenceType { name = customerDisplayName, Value = customerId },
CurrencyRef = new ReferenceType { type = "Currency", Value = "CAD" },
TotalAmt = amount,
TotalAmtSpecified = true,
DepositToAccountRef = new ReferenceType() { Value = "5" },
Line = invoiceLines, // Variable is for debugging purposes - it should be inline or call a method.
PaymentRefNum = reference,
PaymentMethodRef = new ReferenceType()
{
name = paymentName,
Value = paymentId,
}
};
DataService dataService = new DataService(serviceContext);
Payment results = dataService.Add<Payment>(payment);
var json = JsonConvert.SerializeObject(results);
Console.Write(json); // TODO - Use break point/ or write response somewhere for clues.
I can't get my group , in create the campaigns
var campaign = Manager.Campaigns.AddAsync(new Campaign
{
Settings = _campaignSettings,
Recipients = new Recipient
{
ListId = ListID,
SegmentOptions = new SegmentOptions
{
Match = Match.Any,
Conditions = new List<Condition>
{
new Condition{Type=ConditionType.Interests ,Field = "interests-bfb01be5fa",Operator = Operator.InterestContains, Value = "5cde411d26" }
}
}
},
Type = CampaignType.Regular,
}).Result;
The Picture group doesn't select:
The Picture i manually selected the group:
I have a answer .
It work after changed the value
Value = new Dictionary<int , string>{ { 1 , "5cde411d26" } } } // If Have two group , can be 2 , "Group ID"
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>() {...}
Using C# with Lambda(or some method), how can I merge different informations(from columns) of the same intituition name ? They can have some common and some different fields.
See a example below:
Have you tried the Group By instruction ?
Here is an other stackoverflow about this : Group by in LINQ
{
List<Instituition> Instituitions = new List<Instituition>() {
new Instituition { Name = "Name1", Alias = "",Area="" },
new Instituition { Name = "Name1", Alias = "Alias1",Area="" },
new Instituition { Name = "name2", Alias = "Audi" ,Area="444"},
new Instituition { Name = "Name1", Alias = "",Area="Garden" }};
var results = Instituitions.GroupBy(i => i.Name,
i => i.Alias,
(key, g) => new
{
Name = key,
Alias = g.Where(a =>a != "").FirstOrDefault(),
Area = Instituitions.Where(a=>a.Name == key && a.Area != "").Select(a=>a.Area).FirstOrDefault()
}
);
}
class Instituition
{
internal string Name;
internal string Alias;
internal string Area;
}
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)};