I am trying to set the value of object property which is of type List and initialise it by using a foreach to add the items to the list e.g.
var sessionPlanner = new SessionPlannerDTO()
{
Age = "",
NumberOfPlayers = session.numberOfPlayers.Value,
MedicalInformation = "",
PlayerNeeds = "",
SessionDate = session.daySessionDate.Value,
Location = session.Location.locationName,
PracticeView = new List<PracticeViewDTO>(foreach(var practice in session.Sessions){
new PracticeViewDTO(){AbilityLevel = practice.ActivityPlan.abilityLevel.Value,
ActivityUrl = practice.ActivityPlan.activityUrl,
EquipmentNeeds = practice.ActivityPlan.equipmentNeeds,
FacilityNeeds = practice.ActivityPlan.activityNeeds,
HealthAndSafety = practice.ActivityPlan.healthAndSafetyIssues,
SessionTitle = practice.ActivityPlan.activityName
};
})
};
PracticeView is what I am trying to achieve by making it a list without doing the below:
var practiceViewList = new List<PracticeViewDTO>();
foreach(var practice in session.Sessions)
{
var practiceX = new PracticeViewDTO()
{
AbilityLevel = practice.ActivityPlan.abilityLevel.Value,
ActivityUrl = practice.ActivityPlan.activityUrl
};
practiceViewList.Add(practiceX);
}
You can't use other code than assignments in object or collection initializers. So your foreach() there won't compile.
Use session.Sessions.Select() to map the source entities to your DTO, and ToList() to create a list of the result:
sessionPlanner = new SessionPlannerDTO
{
Age = "",
// ...
PracticeView = session.Sessions.Select(s =>
new PracticeViewDTO
{
AbilityLevel = s.ActivityPlan.abilityLevel.Value,
// ...
}).ToList()
};
You also may want to consider using AutoMapper, instead of hand-writing mapping code.
Related
I try to join two tables:
var data = from request in context.Requests
join account in context.AutolineAccts
on request.PkRequest.ToString() equals account.AccountCode
select new
{
ID = request.PkRequest.ToString(),
Location = request.FkLocation,
RequestDate = request.RequestDate.Value,
Requestor = request.FkRequestor,
DebitorNr = request.FkDebitor.ToString(),
NewDebit = request.Debit.ToString(),
ApprovalStatus = request.ApprovalStatus.ToString(),
RecommendationStatus = request.RecommendationStatus.ToString(),
DebitorName = account.CustomerSname,
Limit = account.CreditLimit
};
Now I want to filter the result set depending on the status of the user:
// Accounting user
if (ActiveDirectoryHelper.CheckIfUserIsInADGroup(userLogin, AdGroups.ACCOUNTING) )
req = data.Where(x => x.RecommendationStatus == null).ToList();
// After sales manager
else if (ActiveDirectoryHelper.CheckIfUserIsInADGroup(userLogin, AdGroups.SAV_LEADERS))
req = data.OrderByDescending(x => x.ID).ToList();
// Everybody else
else
req = data.OrderByDescending(x => x.PkRequest).ToList();
And that's where I'm stuck. When I don't have the join and only retrieve a "Request" type, I can just declare a list of Requests
List<Requests> req;
But with that combination of Requests and AutolineAccts I would have to declare and initialize a list of "items" (req) to assign the result set to in the if-else segments. But I don't know how that anonymous variable should look like.
Later on I have to map the result set to a list of my IndexViewModels:
foreach (var item in req)
viewModel.CLModels.Add(new IndexViewModel
{
ID = item .PkRequest.ToString(),
Location = item .FkLocation,
RequestDate = item .RequestDate.Value,
Requestor = item .FkRequestor,
DebitorNr = item .FkDebitor.ToString(),
NewDebit = item .Debit.ToString(),
ApprovalStatus = item .ApprovalStatus.ToString(),
RecommendationStatus = item .RecommendationStatus.ToString(),
DebitorName = item.CustomerSname,
Limit = item.CreditLimit
});
Any idea to solve this issue?
IEnumerable<dynamic> result = from request in requests
join account in accounts
on request.id equals account.id
select new {id=request.id, name=account.name};
Keeps the accessors, can further be queried and be return type of a method.
One option to avoid dynamic is to give the compiler an example of your anonymous type:
var req = new[]
{
new
{
ID = "",
Location = "",
RequestDate = DateTime.Now,
Requestor = "",
DebitorNr = "",
NewDebit = "",
ApprovalStatus = "",
RecommendationStatus = "",
DebitorName = "",
Limit = 0
}
}.ToList();
Obviously you should adjust the values to match the type that you expect.
It's a bit ugly, but personally I'd prefer that over using dynamic typing.
If you wanted to avoid actually allocating the array and the list, you could use type inference in a fairly horrible way:
// Simple method that returns null, but enables type inference
public static List<T> ProvideNullList(T sample) => null;
// Call it when you want to declare your variable:
var sample = new
{
ID = "",
Location = "",
RequestDate = DateTime.Now,
Requestor = "",
DebitorNr = "",
NewDebit = "",
ApprovalStatus = "",
RecommendationStatus = "",
DebitorName = "",
Limit = 0
};
var req = GenericHelpers.ProvideNullList(sample);
That does still allocate the sample, but it's at least a bit better than the array and list as well.
I'm having trouble with Nested aggregations. Here is how I define my aggregations and the json equivalent I get:
var aggregations = new AggregationDictionary();
var nestedAgg = new AggregationContainer
{
Nested = new NestedAggregation("some_name1")
{
Path = "users",
Aggregations = new TermsAggregation("some_name2")
{
Field = "users.name.keyword",
Size = 100,
Order = new List<TermsOrder> { new TermsOrder() { Key = "_term", Order = SortOrder.Descending } }
}
};
aggregations[aggKey] = nestedAgg;
searchRequest.Aggregations = aggregations;
The above is translated to the following json. it only shows the path and no aggregations definition:
{
"aggs": {
"some_name1": {
"nested": {
"path": "users"
}
}
}
Using AggregationDictionary and AggregationContainer directly is a little awkward; You can do so like this
var aggregations = new AggregationDictionary();
var aggKey = "some_name1";
AggregationContainer nestedAgg = new NestedAggregation("some_name1")
{
Path = "users",
Aggregations = new TermsAggregation("some_name2")
{
Field = "users.name.keyword",
Size = 100,
Order = new List<TermsOrder>
{
new TermsOrder() { Key = "_term", Order = Nest.SortOrder.Descending }
}
}
};
aggregations[aggKey] = nestedAgg;
var searchRequest = new SearchRequest<Test>();
searchRequest.Aggregations = aggregations;
client.Search<Test>(searchRequest);
Here we use the implicit conversion from AggregationBase (the base class that aggregations derive from) to AggregationContainer to assign a NestedAggregation to a variable of type AggregationContainer.
This implicit conversion also sets the Aggregations and Meta properties on AggregationContainer, which is why your original attempt doesn't quote work correctly.
Below is the code I'm working with to pass multiple line items to create sales order through GP Web service. I can pass single Line Item without any problem , but when I pass multiple Items it is only taking the last one. The array has around 5 Item ID and I'm passing fixed Quantity as 15, Need to make both dynamic. But for the testing purpose I'm trying like this. I know the problem with the creation/initialization of some web service objects. As novice to the entire set of things I couldn't find the exact problem.
C# Code
CompanyKey companyKey;
Context context;
SalesOrder salesOrder;
SalesDocumentTypeKey salesOrderType;
CustomerKey customerKey;
BatchKey batchKey;
// SalesOrderLine salesOrderLine;
ItemKey orderedItem;
Quantity orderedAmount;
Policy salesOrderCreatePolicy;
DynamicsGPClient wsDynamicsGP = new DynamicsGPClient();
wsDynamicsGP.ClientCredentials.Windows.ClientCredential.UserName = "Admin";
wsDynamicsGP.ClientCredentials.Windows.ClientCredential.Password = "pass";
wsDynamicsGP.ClientCredentials.Windows.ClientCredential.Domain = "Gp";
System.ServiceModel.WSHttpBinding binding;
binding = new System.ServiceModel.WSHttpBinding(System.ServiceModel.SecurityMode.None);
context = new Context();
companyKey = new CompanyKey();
companyKey.Id = (1);
context.OrganizationKey = (OrganizationKey)companyKey;
salesOrder = new SalesOrder();
salesOrderType = new SalesDocumentTypeKey();
salesOrderType.Type = SalesDocumentType.Order;
salesOrder.DocumentTypeKey = salesOrderType;
customerKey = new CustomerKey();
customerKey.Id = "121001";
salesOrder.CustomerKey = customerKey;
batchKey = new BatchKey();
batchKey.Id = "RMS";
salesOrder.BatchKey = batchKey;
// SalesOrderLine[] orders = new SalesOrderLine[6];
SalesOrderLine[] lines = { };
for (int i = 1; i < 5; i++)
{
SalesOrderLine salesOrderLine = new SalesOrderLine();
orderedItem = new ItemKey();
orderedItem.Id = Arr[i].ToString();
salesOrderLine.ItemKey = orderedItem;
orderedAmount = new Quantity();
orderedAmount.Value = 15;
salesOrderLine.Quantity = orderedAmount;
lines = new SalesOrderLine[] { salesOrderLine };
MessageBox.Show(lines.Count().ToString());
}
salesOrder.Lines = lines;
//salesOrder.Lines = orders;
salesOrderCreatePolicy = wsDynamicsGP.GetPolicyByOperation("CreateSalesOrder", context);
wsDynamicsGP.CreateSalesOrder(salesOrder, context, salesOrderCreatePolicy);
if (wsDynamicsGP.State != CommunicationState.Faulted)
{
wsDynamicsGP.Close();
}
MessageBox.Show("Success");
lines = new SalesOrderLine[] { salesOrderLine }; will recreate the lines array object each time meaning you loose any previously added objects. Effectively only the final object in the loop is actually added.
Try using a List<T> like so:
SalesOrderLine[] lines = { }; Becomes List<SalesOrderLine> lines = new List<SalesOrderLine>();
lines = new SalesOrderLine[] { salesOrderLine }; Becomes: lines.Add(salesOrderLine);
If its important you end up with an array as the input:
salesOrder.Lines = lines; Becomes: salesOrder.Lines = lines.ToArray();
I am trying get data from the xml. Below is the code which
gets data from the XDocument and return list<t>.
However, p.Element("Sponsor") can sometimes be null. How can I check for the null values
var atClauseList = doc.Descendants(CLAUSE_GROUP_TAG).Descendants(AT_CLAUSE_TAG).Select(p => new AtClause()
{
ClauseNumber = (string)p.Element("Number"),
Sponsors = p.Element("Sponsor").Elements(SPONSOR_TAG).Select(y => y.Value)
.ToList(),
Page = p.Element("Sponsor").Element("aItem").Element("AmendText").Element("Page").ElementValueNull(),
Line = p.Element("Sponsor").Element("aItem").Element("AmendText").Element("Line").ElementValueNull(),
LineText = p.Element("Sponsor").Element("aItem").Element("AmendText").Nodes().OfType<XText>().FirstOrDefault().XTextValueNull(),
ItalicText = p.Element("Sponsor").Element("aItem").Element("AmendText").Element("Italic").ElementValueNull(),
ParaList = p.Element("Sponsor").Element("aItem").Element("AmendText").Elements("Para").Select(L => new Para
{
ParaText = (string)L,
Number = ((System.Xml.Linq.XElement)(L)).AttributeValueNull("Number"),
Quote = ((System.Xml.Linq.XElement)(L)).AttributeValueNull("Quote"),
}
).ToList()
}).ToList();
move your code out of an object initializer, and add some logic to it:
var atClauseList = new List<AtClause>();
foreach(var item in doc.Descendants(CLAUSE_GROUP_TAG).Descendants(AT_CLAUSE_TAG))
{
var atClause = new AtClause();
atClause.ClauseNumber = (string)item.Element("Number");
var sponsor = item.Element("Sponsor");
if (sponsor != null)
{
atClause.Sponsors = sponsor.Elements(SPONSOR_TAG).Select(y => y.Value).ToList();
atClause.Page = sponsor.Element("aItem").Element("AmendText").Element("Page").ElementValueNull();
atClause.Line = sponsor.Element("aItem").Element("AmendText").Element("Line").ElementValueNull();
atClause.LineText = sponsor.Element("aItem").Element("AmendText").Nodes().OfType<XText>().FirstOrDefault().XTextValueNull();
atClause.ItalicText = sponsor.Element("aItem").Element("AmendText").Element("Italic").ElementValueNull();
atClause.ParaList = sponsor.Element("aItem").Element("AmendText").Elements("Para").Select(L => new Para
{
ParaText = (string)L,
Number = ((System.Xml.Linq.XElement)(L)).AttributeValueNull("Number"),
Quote = ((System.Xml.Linq.XElement)(L)).AttributeValueNull("Quote"),
}).ToList();
atClauseList.Add(atClause);
}
You can use sequences rather than leaving the IEnumerable immediately:
var value = (string)p.Elements("Sponsor")
.Elements("aItem")
.Elements("AmendText")
.Elements("Page")
.SingleOrDefault()
I have this Linq and what I tried to do to set the element into the hashset and doesn't work what's missing?? When I debugged the result always show me the property empty
var estructura = (from result in query
select new Estructura()
{
IdEstructura = result.Servicio.Campana.Cliente.Direccion.IdDireccion,
Descripcion = result.Servicio.Campana.Cliente.Direccion.Descripcion,
lstEstructurasHijos = new HashSet<Estructura>().Select(
C => new Estructura()
{
IdEstructura = result.Servicio.Campana.Cliente.IdCliente,
Descripcion = result.Servicio.Campana.Cliente.Descripcion,
lstEstructurasHijos = new HashSet<Estructura>().Select(
CA => new Estructura()
{
IdEstructura = result.Servicio.Campana.IdCampana,
Descripcion = result.Servicio.Campana.Descripcion,
lstEstructurasHijos = new HashSet<Estructura>().Select(
S => new Estructura()
{
IdEstructura = result.Servicio.IdServicio,
Descripcion = result.Servicio.Descripcion,
lstEstructurasHijos = new HashSet<Estructura>()
})
})
})
});
You are misusing the selecton the HashSet like this one:
new HashSet<Estructura>().Select(//... Means take from that new HashSet....
Of course a new HashSet won't contain anything. This's why you get empty HashSets.