I have three lists which contains three same properties in each collection. I want to combine a result into one collection. Ex classes structure is as below
public class Order
{
public int ProductId { get; set; }
public int CustomerId { get; set; }
public int OrderId { get; set; }
// Few other Properties of OrderDetail
}
public class PaymentDetail
{
public int ProductId { get; set; }
public int CustomerId { get; set; }
public int OrderId { get; set; }
// Few other Properties form PaymentDetail
}
public class CouponUsageDetail
{
public int ProductId { get; set; }
public int CustomerId { get; set; }
public int OrderId { get; set; }
// Few other Properties form CouponUsageDetail
}
This type of output is coming from one API service where each class is in form of list object (JSON format) and we need to perform some operations on this. All three properties (ProductId,CustomerId, OrderId) contains same values in each collection which means all three properties are repeated in each collection. We need to perform some look ups on these collections. So in normal way what we can do with it is as - start a foreach from Order list and filter all three matching properties of PaymentDetail and CouponUsageDetail. But it would be costlier in term of performance when the data size is increased. So thought of making nesting structure upfront and avoid lookups. If we make the output nested as below this will help to avoid lookups on PaymentDetail and CouponUsageDetail.
Ex - we are receiving JOSN in below format
{"Orders":[{"ProductId":301,"CustomerId":101,"OrderId":201},{"ProductId":701,"CustomerId":501,"OrderId":601}],"PaymentDetails":[{"ProductId":301,"CustomerId":101,"OrderId":201},{"ProductId":701,"CustomerId":501,"OrderId":601}],"CouponUsageDetails":[{"ProductId":301,"CustomerId":101,"OrderId":201},{"ProductId":701,"CustomerId":501,"OrderId":601}]}
and with this output we want to form object as
public class OrderDetails
{
public int ProductId { get; set; }
public int CustomerId { get; set; }
public int OrderId { get; set; }
// Few other Properties of OrderDetail
List<PaymentDetail> PaymentDetail { get; set; }
List<CouponUsageDetail> CouponUsageDetail { get; set; }
}
Can you guide, what would be the best optimum usage of linq where we can combine all these three matching properties and make it just one nested structure better?
Thank You!
Note: I know this structure needs to be normalized but please ignore the normalization rule here as this is not in our control.
What are you describing sounds like two standard multi-key LINQ group joins. They are quite efficient (LINQ to Objects implementation uses prepared fast hash based lookups), so no further optimizations are needed:
var orderDetails = (
from o in data.Orders
join p in data.PaymentDetails
on new { o.ProductId, o.CustomerId, o.OrderId }
equals new { p.ProductId, p.CustomerId, p.OrderId }
into orderPaymentDetails
join c in data.CouponUsageDetails
on new { o.ProductId, o.CustomerId, o.OrderId }
equals new { c.ProductId, c.CustomerId, c.OrderId }
into orderCouponUsageDetails
select new OrderDetails
{
ProductId = o.ProductId,
CustomerId = o.CustomerId,
OrderId = o.OrderId,
// Few other Properties of OrderDetail
PaymentDetail = orderPaymentDetails.ToList(),
CouponUsageDetail = orderCouponUsageDetails.ToList(),
})
.ToList();
There seems to be a number of questions here combined, I'll try to work through them:
Data Models and Deserialization
With respect to generating a single structure from your API response, I would recommend using the Newtonsoft.Json libraries, available on NuGet Json.NET. They will allow you to deserialize the response from your API, into a single object, which given the sample you provided, should contain a collection of each of your models, Order, PaymentDetail, CouponUsageDetail:
public class APIResponceContainer
{
[JsonProperty("Orders")]
public List<Order> Orders { get; set; }
[JsonProperty("PaymentDetails")]
public List<PaymentDetail> PaymentDetails { get; set; }
[JsonProperty("CouponUsageDetails")]
public List<CouponUsageDetail> CouponUsageDetails { get; set; }
public APIResponceContainer()
{
Orders = new List<Order>();
PaymentDetails = new List<PaymentDetail>();
CouponUsageDetails = new List<CouponUsageDetail>();
}
}
Be aware to add the required attributes to each of your models as so:
public class Order
{
[JsonProperty("ProductId")]
public int ProductId { get; set; }
[JsonProperty("CustomerId")]
public int CustomerId { get; set; }
[JsonProperty("OrderId")]
public int OrderId { get; set; }
}
Deserialization then happens from your JSON string, as such:
StringReader stringReader = new StringReader(myJSONString);
JsonSerializer js = JsonSerializer.Create();
APIResponceContainer APIResponce = (APIResponceContainer)js.Deserialize(stringReader, typeof(APIResponceContainer));
Queries
As discussed in the comments, your data is unfortunately in terrible need of normalization. However, what I have inferred is that you would like to produce a flat structure, maintaining the "Few other Properties" and "key properties", for a combination of Order, PaymentDetail and CouponUsageDetail. You can use Linq for this, importantly I would recommend you choose yourself a "Primary Key". In other words, one property that can independently tie all the others together. In the example below, I have choose OrderID since it should be unique (?):
var flatSequence =
from order in APIResponce.Orders
join coupon in APIResponce.CouponUsageDetails on order.OrderId equals coupon.OrderId
join payment in APIResponce.PaymentDetails on order.OrderId equals payment.OrderId
select new
{
// Here extract all the properties you care about
OrderID = order.OrderId,
Customer = order.CustomerId,
Product = order.ProductId,
// All the "other Properties" ?
BankDetail = payment.PaymentOnlyProperty
};
Here I have extracted to var, however if you know the final flat structure you would like, of course determine a class of your own to receive the result.
Please comment if there is any questions.
You can use inheritance.
public class ResultCollection : Collection1
{
List<Collection2> Collection2s { get; set; }
List<Collection3> Collection3s { get; set; }
}
and then
var result = new ResultCollection {
PropId1 = Collection1.PropId1,
PropId2 = Collection1.PropId2,
...
Collection2s = Collection2,
Collection3s = Collection3
}
An automapper can be helpful here.
https://docs.automapper.org/en/stable/
I have solution, but i am not sure if it's ok for you. It depends on data format that you have on the begining.
solution:
class Program
{
static void Main(string[] args)
{
var collection1 = new Collection1() { PropId1 = 1, PropId2 = 2, PropId3 = 3 };
var list2 = new List<Collection2>()
{
new Collection2
{
PropId1 = 11,
PropId2 = 22,
PropId3 = 33
},
new Collection2
{
PropId1 = 22,
PropId2 = 33,
PropId3 = 44
}
};
var list3 = new List<Collection3>()
{
new Collection3
{
PropId1 = 111,
PropId2 = 222,
PropId3 = 333
},
new Collection3
{
PropId1 = 222,
PropId2 = 333,
PropId3 = 444
}
};
var result = new ResultCollection(collection1, list2, list3);
//or
var result2 = new ResultCollection(collection1) //but in this case you have to change your constructor
{
Collection2s = list2,
Collection3s = list3
};
Console.ReadLine();
}
}
public class Collection1
{
public int? PropId1 { get; set; }
public int? PropId2 { get; set; }
public int? PropId3 { get; set; }
}
public class Collection2
{
public int? PropId1 { get; set; }
public int? PropId2 { get; set; }
public int? PropId3 { get; set; }
}
public class Collection3
{
public int? PropId1 { get; set; }
public int? PropId2 { get; set; }
public int? PropId3 { get; set; }
}
public class ResultCollection : Collection1
{
public ResultCollection() { }
public ResultCollection(Collection1 collection, List<Collection2> list2, List<Collection3> list3)
{
foreach (PropertyInfo prop in collection.GetType().GetProperties())
{
PropertyInfo prop2 = collection.GetType().GetProperty(prop.Name);
if (prop2.CanWrite)
prop2.SetValue(this, prop.GetValue(collection, null), null);
}
Collection2s = list2;
Collection3s = list3;
}
public List<Collection2> Collection2s { get; set; }
public List<Collection3> Collection3s { get; set; }
}
But can you give an example of input data?
Related
I am trying to make my code more compromised, and use overall less, however currently I'm running into the problem of not being able to send a list of Objects sorted by linq as a parameter.
the problem is in this part of the code:
List<Afspraken> dataAfspraken = new List<Afspraken>();
public Form1()
{
InitializeComponent();
fillListsForLinq();
loadReceptionData();
}
private void fillListsForLinq()
{
dataAfspraken = data.getAfsprakenData();
//here it fills the list with Afspraken objects
}
private void loadReceptionData()
{
private void loadReceptionGrid
var receptionToFinnish =
(from AFspraken in dataAfspraken
where Afspraken.factuur_betaald == true && Afspraken.volledig_afgerond == false
join Users in dataUsers on Afspraken.gekoppelde_klant equals Users.id
select new
{
Id = Afspraken.id,
Klant = Users.gebruikersnaam,
Betaald = Afspraken.factuur_betaald,
Afgerond = Afspraken.volledig_afgerond
}).ToList();
changeDataviewReception(receptionToFinnish);
}
private void changeDataviewReception(List<Object> listData)
{
dgvReceptionData.DataSource = listData
}
the Afspraken class looks like this
public class Afspraken
{
public int id { get; set; }
public bool bevestigd { get; set; }
public DateTime datum { get; set; }
public int gekoppelde_klant { get; set; }
public int gekoppelde_monteur { get; set; }
public string benodigde_hadelingen { get; set; }
public decimal totaalprijs { get; set; }
public bool klaar { get; set; }
public bool factuur_betaald { get; set; }
public bool volledig_afgerond { get; set; }
public string opmerkingen { get; set; }
}
How do I get receptionToFinnish as a parameter into changeDataviewReception?
receptionToFinnish will be a list full of objects of an anonymous type. But your method requires a List<object>. This is now allowed since a list is not a variant type.
Say for example that you have a list of bananas and want to give it to someone that wants a list of fruits. This will not work since that other person might try to add an orange to the list of bananas.
To fix this, cast the values to object explicitly, for example:
select new
{
Id = Afspraken.id,
Klant = Users.gebruikersnaam,
Betaald = Afspraken.factuur_betaald,
Afgerond = Afspraken.volledig_afgerond
} as object
This is what I do to set more properties value after query. Please see the method GetMoreData(), it will be executed for every detail loop and starts causing performance issue.
Is there a better way of doing this? I know the class constuctor that execute during object instantiate. But in this case, more properites must be set after database query.
Note: This is a cut-down version of source code focusing on important part only, please accept typo error, if any.
public class OrderHeader
{
// Real database column
public string OrderId { get; set; }
public string ColA { get; set; }
public string ColB { get; set; }
public string PostCode { get; set; }
public decimal TotalWeight { get; set; }
public decimal TotalShipmentFee { get; set; }
}
public class OrderDetail
{
// Real database column
public string OrderId { get; set; }
public string OrderLine { get; set; }
public decimal Weight { get; set; }
public decimal ShipmentFee { get; set; }
// mock-up column for displaly only, not exist in datababse
public string ColA { get; set; }
public string ColB { get; set; }
}
public static List<OrderDetail> GetByItemClass(string itemClass)
{
using (IDbConnection db = new MySqlConnection(connectionStringGoesHere)) {
string sqlCmd = #"SELECT * FROM orderdetail WHERE ItemClass = #ItemClass";
List<OrderDetail> orderDetails = db.Query<OrderDetail>(sqlCmd, new {
#ItemClass = itemClass
}).ToList();
// Get or build or calculate additional properties
for (int i = 0; i < orderDetails.Count; i++) {
orderDetails[i] = GetMoreData(orderDetails);
}
return orderDetails;
}
}
public static List<OrderDetail> GetByItemType(string itemType)
{
using (IDbConnection db = new MySqlConnection(connectionStringGoesHere)) {
string sqlCmd = #"SELECT * FROM orderdetail WHERE ItemType = #ItemType";
List<OrderDetail> orderDetails = db.Query<OrderDetail>(sqlCmd, new {
#ItemType = itemType
}).ToList();
// Get or build or calculate additional properties
for (int i = 0; i < orderDetails.Count; i++) {
orderDetails[i] = GetMoreData(orderDetails);
}
return orderDetails;
}
}
public static OrderDetail GetMoreData(OrderDetail orderDetail)
{
// Performance problem: Need to query order header for every
// single loop even multiple records having the same OrderId
OrderHeader orderHeader = OrderHeaderDal.GetById(orderDetail.OrderId);
// Directly map value
orderDetail.ColA = orderHeader.ColA;
orderDetail.ColB = orderHeader.ColB;
// Calculate value
if (orderHeader.PostCode == "0") {
orderDetail.ShipmentFee = orderDetail.Weight * 1.15;
// More complex to get value from another table
} else {
// Might also cause performance issue for many query loop
Rate rate = RateDal.GetByPostCode(orderHeader.PostCode);
orderDetail.ShipmentFee = orderDetail.Weight * rate.RatePerKg;
}
return orderDetail;
}
I have a list created from a stored procedure using EF6.0
I have also created 3 classes
public class Resas
{
public string todo{ get; set; }
public string prop { get; set; }
public string Code { get; set; }
public string statusCode { get; set; }
public string checkin { get; set; }
public string checkout { get; set; }
public List<profiles> profiles { get; set; }
}
public class profiles
{
public string action { get; set; }
public string id { get; set; }
public string profileType { get; set; }
public string title { get; set; }
public string firstName { get; set; }
public string middleName { get; set; }
public string lastName { get; set; }
public List<emailAddresses> emailAdresses { get; set; }
}
public class emailAddresses
{
public string emailAddress { get; set; }
public string emailAddress2 { get; set; }
}
I am doing a for-loop in the list and I need to get certain columns and put it in the array (I will put two, to keep it simple)
myEntities db = new myEntities();
List<rev_Result> revList = new List<rev_Result>();
revList.Clear();
revList = db.rev().ToList();
for (int i = 0; i < revList.Count(); i++)
{
Resas resas = new Resas();
profiles[] profiles = new profiles[1];
resas.todo = revList[i].todo;
resas.profiles[0].lastName = revList[i].lastName;
}
I am not familiar with C# as you can see from the psedo-code above.
I cannot figure out how to feed the Resas with data and then its Profile with data and then move to the next Resas entry.
Any help appreciated.
That's fairly simple using Linq:
Resas resas = new Resas();
resas.profiles = revList
.Select(x => new profiles() { action = x.todo, lastName = x.lastName })
.ToList();
What's happening here is: You loop through every entry in revList and get your wanted data structure (that's what Select is doing). x refers to the current entry in the loop, while the stuff to the right side of the arrow is you 'output': a new instance of your profiles class with the members assigned accordingly. The result of all of this is then converted to a list (before ToList(), think of it as a recipe to create the list) and assigned to resas.profiles.
By the way, a word on conventions: Usually, in C#, you would give your classes a name that starts with a capital letter. Also, your profiles class seems to contain data of exactly one profile, so a better name might be Profile. This also makes your data structure more clear, since List<profiles> seems to be a list of lists of profiles - but that's not what it actually is, is it?
Furthermore, Members generally start with a capital letter as well, so instead of action, lastName, you'd have: Action and LastName.
You can try with Linq. This is the code that should solve your issue, but Resas class doesn't have action property:
List<Resas> ls = revList.Select(x => new Resas() {
action = x.todo,
profiles = new List<profiles>() {
new profiles { lastName = x.lastName }
}
).ToList();
If you need to use action property of inprofiles` class:
List<Resas> ls = revList.Select(x => new Resas() {
profiles = new List<profiles>() {
new profiles {
action = x.todo,
lastName = x.lastName
}
}
).ToList();
I am trying to create a list of Queues that are displayed by Queue Category. Each Queue Category is assigned an Enum value as such.
public enum QueueCategory
{
None=0,
Critical=1,
High=2,
Orphaned=3,
Missing=4
}
And for each Category, I want to then display these fields.
public class QueueInformation
{
public string Name { get; set; }
public Decimal PercentOfThreshold { get; set; }
public string Host { get; set; }
public DateTime OldestArrival { get; set; }
public QueueCategory Category { get; set; }
}
}
How would I go about linking these two pages so that QueueInformation is displayed by QueueCategory?
IEnumerable<QueueInformation> infos = ...;
foreach (var categoryGroup in infos.GroupBy(i => i.Category))
{
Console.WriteLine("Current category: {0}", categoryGroup.Key);
foreach (var queueInfo in categoryGroup)
{
Console.WriteLine(queueInfo.Name /*...*/);
}
Console.WriteLine("==========================");
}
I assume you want a source ordered by the QueueCategory:
IEnumerable<QueueInformation> list = new BindingList<QueueInformation>();
var orderedList = from l in list orderby l.Category select l;
Hope this helps
I have the following class:
public class EntityJESummary
{
public int JEGroupingId { get; set; }
public int PartnershipId { get; set; }
public int JEId { get; set; }
public DateTime BookingDate { get; set; }
public DateTime EffectiveDate { get; set; }
public bool Allocated { get; set; }
public int JEEstate { get; set; }
public float Debit { get; set; }
public float Credit { get; set; }
public string JEComments { get; set; }
public EntityJESummary()
{
}
}
And here I'm using Linq to filter out DataRows from a source. I'm trying to fit information from this datasource into this new holder type class.
Any suggestions?
_dttMasterViewTransaction = dtsTransaction.Tables["tblTransaction"];
var datos = _dttMasterViewTransaction.AsEnumerable()
.Where(r => r["JEID"] == FundsID)
.Select(new EntityJESummary ???
Notice where I'm using r["foo"], I'm fetching data from each DataRow. I need to get specific rows and fit them into specific properties of my holder class.
Also, in the data table, there might be many rows for a single JEId, so I'd like to grab each Debit from each datarow and Sum it into the float Debit property.
Any suggestions would be very much appreciated. :)
Untested but try something similar to what you did with the Where clause:
var datos = _dttMasterViewTransaction.AsEnumerable()
.Where(r => r["JEID"] == FundsID)
.Select(r => new EntityJESummary {
JEGroupingId = r["JEGroupingId"],
PartnershipId = r["PartnershipId"],
.....
} );
You can make use of Object Initalizers.
_dttMasterViewTransaction = dtsTransaction.Tables["tblTransaction"];
var datos = _dttMasterViewTransaction.AsEnumerable()
.Where(r => r["JEID"] == FundsID).Select(r =>
new EntityJESummary() {
JEGroupingId = r["JEID"],
PartnershipId = r["PartnershipId"]
};