I am parsing XML to create an object. I am just unsure as to how to cast the var lotConfig into a LotCOnfig object.
I have tried :
var lotConfig = (LotConfig) xml.Descendants("LOT_CONFIGURATON").Select(
and
return lotConfig as LotConfig
But None have worked so far.
public LotConfig GetLotConfigData(FileInfo lotFile)
{
var xml = XElement.Load(lotFile.FullName);
var lotConfig = xml.Descendants("LOT_CONFIGURATON").Select(
lot => new LotConfig
{
LotNumber = (string) lot.Element("LOT_NUMBER").Value,
LotPartDescription = lot.Element("PART_DESCRIPTION").Value,
LotDate = lot.Element("LOT_DATE").Value,
ComponentList = lot.Descendants("ASSY_COMPONENT").Select(ac => new LotComponent
{
PartNumber = ac.Descendants("COMPONENT_PART_NUMBER").FirstOrDefault().Value,
ArtworkNumber = ac.Descendants("ARTWORK_NUMBER").FirstOrDefault().Value,
RefDesignator = ac.Descendants("REFERENCE_DESIGNATOR").FirstOrDefault().Value
}).ToList()
});
return lotConfig as LotConfig;
}
Currently lotConfig is of type IEnumerable<LotConfig>, you need to call .FirstOrDefault() at the end of your LINQ to make it return single LotConfig type :
var lotConfig = xml.Descendants("LOT_CONFIGURATON")
.Select(
lot => new LotConfig
{
LotNumber = (string) lot.Element("LOT_NUMBER").Value,
LotPartDescription = lot.Element("PART_DESCRIPTION").Value,
LotDate = lot.Element("LOT_DATE").Value,
ComponentList = lot.Descendants("ASSY_COMPONENT").Select(ac => new LotComponent
{
PartNumber = ac.Descendants("COMPONENT_PART_NUMBER").FirstOrDefault().Value,
ArtworkNumber = ac.Descendants("ARTWORK_NUMBER").FirstOrDefault().Value,
RefDesignator = ac.Descendants("REFERENCE_DESIGNATOR").FirstOrDefault().Value
}).ToList()
})
.FirstOrDefault();
Related
I have two lists of same type with different key value pairs,
List1 has "isPermanent = true" and List2 has false value and also
List1 has an extra key "nextVacationDate".
Im trying to do union of these as below but im afraid I will still get the duplicates because of different values. I need to merge both lists in to one list and order by List1 first (Permanent employees first)..is there a better way to do this using LINQ?
public newList1 List1(string abcd)
{
var result = serviceMethod1(abcd);
var newList1 = new List<emp>();
if (result == null) return null;
newList.AddRange(
result.Select(x => new Model
{
firstName = x.FName,
secondName = x.SName,
address = x.Address,
employeeId = x.EmpId,
isPermanent = true,
nextVacationDate =x.VacDt,
salary = x.Bsalary
}));
return newList1;
}
public newList2 List2(string defg)
{
var result = serviceMethod2(defg);
var newList2 = new List<emp>();
if (result == null) return null;
newList.AddRange(
result.Select(x => new Model
{
firstName = x.FName,
secondName = x.SName,
address = x.Address,
employeeId = x.EmpId,
isPermanent = false,
salary = x.Bsalary
}));
return newList2;
}
private List<emp> EmployyeList(List<emp> newList1, List<emp> newList2)
{
var sortedEmpList1 = newList1.OrderBy(i => i.Fname);
var sortedEmpList2 = newList2.OrderBy(i => i.Fname);
List<MeterModel> combinedList = newList1.Union(newList2) as List<emp>;
return combinedList;
}
You can filter the 2nd list to avoid duplicates:
newList1.Union(newList2.Where(emp2 => !newList1.Any(emp1 => emp1.employeeId == emp2.employeeId)))
In my model I have public JObject GenericData { get; set; } for this property I need to create Jobject in my Controller that will pass data to this property. I have done this but now I need to sort data inside Jobject and I don't know how to do it. There is no sort function for Jobject out of box.
My code in controller looks like this.
var attributes = _context.AttributeRecords.Include(a => a.Attribute);
var queryRecords = attributes.Select(l => new
{
RecordId = l.RecordId,
ProjectId = l.ProjectId,
Attribute = l.Attribute.Description,
Value = l.Value,
InfoId = l.InfoId
}).ToList();
var recordsValues = queryRecords.Where(b => b.InfoId == i.InfoId).ToList();
var jObjectValues = new JObject();
foreach (var n in recordsValues)
{
if (n.Value.Contains(","))
{
var stringToSplit = n.Value;
var stringValues = stringToSplit.Split(',');
List<string> arr = new List<string>();
var allValues = "";
foreach (var d in stringValues)
{
var values = await _context.AttributeValues.FirstOrDefaultAsync(v => v.Key == n.Value);
arr.Add(values != null ? values.Description : d);
allValues = string.Join(",", arr);
}
jObjectValues.Add(n.Attribute, allValues);
}
else
{
var values = await _context.AttributeValues.FirstOrDefaultAsync(v => v.Key == n.Value);
jObjectValues.Add(n.Attribute, values != null ? values.Description : n.Value);
}
i.GenericData = jObjectValues;
}
You don't need to sort Jobject you can do it like this:
var queryRecords = attributes.Select(l => new
{
RecordId = l.RecordId,
ProjectId = l.ProjectId,
Attribute = l.Attribute.Description,
Value = l.Value,
InfoId = l.InfoId
}).OrderBy(o => o.Attribute).ToList();
I hope this will help you.
I'm doing comparisons across databases (with about 20 fields) and I've defined a concrete type to handle the comparison.
If my comparison fails, I want to loop through the individual items in the catch block and provide the user a list errors. Below, I've done it manually through the first few variables. Is there a more efficient way to loop this through all 20 fields? I started with a foreach (Object objectItem .. but not sure if that's the right way to go.
Any thoughts or much needed guidance?
try {
CollectionAssert.IsSubsetOf(orgs, members, "Error Matching testlist Fields");
}
catch
{
//OrgID
var sourceOrgID = orgs.Select(o => o.OrgID);
var destOrgID = members.Select(o => o.OrgID);
var errorList1 = sourceOrgID.Except(destOrgID);
string failedTests = null;
failedTests = string.Join("\n", errorList1);
Assert.IsTrue(0 == failedTests.Length, "The following Org IDs are not contained in the source: \n" + failedTests);
//DealerCode
var sourceDealerCode = orgs.Select(o => o.DealerCode);
var destDealerCode = members.Select(o => o.DealerCode);
var errorList2 = sourceDealerCode.Except(destDealerCode);
failedTests = null;
failedTests = string.Join("\n", errorList2);
Assert.IsTrue(0 == failedTests.Length, "The following Dealer Codes are not contained in the source: \n" + failedTests);
//orgkey
var sourceOrgKey = orgs.Select(o => o.OrgKey);
var destOrgKey = members.Select(o => o.OrgKey);
var errorList3 = sourceOrgKey.Except(destOrgKey);
failedTests = null;
failedTests = string.Join("\n", errorList3);
Assert.IsTrue(0 == failedTests.Length, "The following Org Keys are not contained in the source: \n" + failedTests);
You need reflection to do this:
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
GetColumn(orgList,property.Name);
}
var names = items.Select(x => x.GetType().GetProperty("prpname").GetValue(x));
public IEnumerable<object> GetColumn(List<Item> items, string columnName)
{
var values = items.Select(x =>
x.GetType().GetProperty(columnName).GetValue(x));//u can put your test code heere and you can loop it through object properties
}
you can create a list to store rslt and add results to the list else you can write output in logs files
If I haven't misunderstood your question, you could do something like this using shouldly
[TestMethod]
public void UnitTestExample()
{
var orgs = new Organisation
{
Ids = new List<int>{ 1,3 },
DealerCodes = new List<string> { "foo","bar"}
};
var members = new Organisation
{
Ids = new List<int> { 1,2,3 },
DealerCodes = new List<string> { "foo", "bar", "buzz" }
};
orgs.ShouldSatisfyAllConditions(
() => orgs.Ids.ShouldBe(members.Ids, ignoreOrder: true),
() => orgs.DealerCodes.ShouldBe(members.DealerCodes, ignoreOrder: true)
);
}
The output produced is:
You would still have to specify every property you want to check inside ShouldSatisfyAllConditions but shouldly does all the heavy lifting around comparing the lists and output the differences.
I'm assume that:
orgs is a IQueryable<T1>
members is a IQueryable<T2>
typeof(T1) == typeof(T2)
If so, that function could help:
private static void CompareRecords<TEntity>(IQueryable<TEntity> orgs, IQueryable<TEntity> members)
{
var entityType = typeof (TEntity);
var selectMethod = typeof (Queryable).GetMethods().First(x => x.Name == "Select");
var exceptMethod = typeof(Queryable).GetMethods().First(x => x.Name == "Except");
var toArrayMethod = typeof (Enumerable).GetMethods().First(x => x.Name == "ToArray");
foreach (var property in entityType.GetProperties())
{
var paramExpr = Expression.Parameter(entityType, "x");
var propExpr = Expression.Property(paramExpr, property.Name);
var delegateType = typeof (Func<,>).MakeGenericType(entityType, property.PropertyType);
var lambdaExpr = Expression.Lambda(delegateType, propExpr, paramExpr);
var parameterizedSelectMethod = selectMethod.MakeGenericMethod(entityType, property.PropertyType);
var parameterizedExceptMethod = exceptMethod.MakeGenericMethod(property.PropertyType);
var source = parameterizedSelectMethod.Invoke(null, new object[] {orgs, lambdaExpr});
var dest = parameterizedSelectMethod.Invoke(null, new object[] {members, lambdaExpr});
var errorList = parameterizedExceptMethod.Invoke(null, new[] {source, dest});
var errorListArray = toArrayMethod.MakeGenericMethod(property.PropertyType).Invoke(null, new[] {errorList});
var failedTests = string.Join("\n", ((IEnumerable)errorListArray).Cast<object>().Select(x => x.ToString()));
Assert.IsTrue(0 == failedTests.Length, $"The following {property.Name} are not contained in the source: \n{failedTests}");
}
}
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 some inbound XML that I am trying to read with LINQ to generate a list of objects:
<SCResponse>
<link href="http://192.168.6.126:8001/affiliate/account/81/notifications?startRow=2&limit=20" rel="next_page" title="Next"/>
<link href="http://192.168.6.126:8001/affiliate/account/81/notifications?startRow=1&limit=20" rel="previous_page" title="Previous"/>
<RecordLimit>20</RecordLimit>
<Notifications>
<Notification href="http://192.168.6.126:8001/affiliate/account/81/notifications/24">
<NotificationDate>2013-03-15T16:41:37-05:00</NotificationDate>
<NotificationDetails>Notification details 1</NotificationDetails>
<Status>New</Status>
<NotificationTitle>Test notification 1</NotificationTitle>
</Notification>
</Notifications>
<RecordsReturned>1</RecordsReturned>
<StartingRecord>1</StartingRecord>
<TotalRecords>1</TotalRecords>
And I've created a simple POCO object to represent a 'Notification':
public class Notification
{
public System.DateTime notificationDate;
public string notificationDetails;
public string status;
public string notificationTitle;
public string href;
}
and I'd like to read the incoming XML and create a list of objects.
List<Notification> notificationList;
XElement x = XElement.Load(new StringReader(result));
if (x != null && x.Element("Notifications") != null)
{
notificationList = x.Element("Notifications")
.Elements("Notification")
.Select(e => new Notification()
.ToList();
}
I'm really unclear about the 'e' part and how to initialize a new Notification object. Can ya help?
e is just a parameter name your passing to the lambda expression, new Notification(). You can use it like this:
notificationList = x.Element("Notifications")
.Elements("Notification")
.Select(e => new Notification()
{
href = (string)e.Attribute("href")
notificationDetails = (DateTime)e.Element("NotificationDate")
notificationDate = (string)e.Element("NotificationDetails")
status = (string)e.Element("Status")
notificationTitle = (string)e.Element("NotificationTitle")
}
.ToList();
Or if you prefer query syntax
notificationList =
(from e in x.Element("Notifications").Elements("Notification")
select new Notification()
{
href = (string)e.Attribute("href")
notificationDetails = (DateTime)e.Element("NotificationDate")
notificationDate = (string)e.Element("NotificationDetails")
status = (string)e.Element("Status")
notificationTitle = (string)e.Element("NotificationTitle")
})
.ToList();
Use object initialization syntax:
notificationList = x.Element("Notifications")
.Elements("Notification")
.Select(e => new Notification()
{
href = (string)e.Attribute("href"),
notificationDate = (DateTime)e.Element("NotificationDate"),
notificationDetails = (string)e.Element("NotificationDetails"),
status = (string)e.Element("Status"),
notificationTitle = (string)e.Element("NotificationTitle")
})
.ToList();
Remember, that you can easily cast objects like XElement and XAttribute into string, int, double, DateTime and more. Complete list can be found here: XElement Type Conversions
notificationList = x.Element("Notifications")
.Elements("Notification")
.Select(e => new Notification()
{
notificationDate = DateTime.Parse(e.Element("NotificationDate").Value),
notificationDetails = e.Element("NotificationDetails").Value,
status = e.Element("Status").Value,
notificationTitle = e.Element("NotificationTitle").Value,
href = e.Attribute("href").Value
}
.ToList();