Member_2_7967608
asked on
Linq query filter List
Hi All
I have below query. I want to filter items and do not want to add new salesOrders() where startDate is Empty.
If startDate is Empty I want to remove it.
req.sOrders = (from document in resp.transaction.quote_dat a.sub_docu ments
let values = document.attributes[0].val ue.ToStrin g().Split( new[] { "**" }, StringSplitOptions.None)
where (document.attributes != null && document.attributes.Length > 0) && !string.IsNullOrWhiteSpace (document. attributes [0].value. ToString() )
select new salesOrder()
{
productCode = values[1],
salesOrderType = values[2],
startDate = String.IsNullOrEmpty(value s[3])? "" : DateTime.Parse(values[3]). ToString(" yyyy-MM-dd "),
pays = !string.IsNullOrWhiteSpace (values[4] ) ? int.Parse(values[4]) : 0,
scns = (from code in values[5].Split(new[] { "$$" }, StringSplitOptions.RemoveE mptyEntrie s) select new scns() { scnCode = (String.IsNullOrEmpty(code )? null : code) }).ToList()
}).ToList();
I have below query. I want to filter items and do not want to add new salesOrders() where startDate is Empty.
If startDate is Empty I want to remove it.
req.sOrders = (from document in resp.transaction.quote_dat
let values = document.attributes[0].val
where (document.attributes != null && document.attributes.Length
select new salesOrder()
{
productCode = values[1],
salesOrderType = values[2],
startDate = String.IsNullOrEmpty(value
pays = !string.IsNullOrWhiteSpace
scns = (from code in values[5].Split(new[] { "$$" }, StringSplitOptions.RemoveE
}).ToList();
ASKER
Thanks that works well. I had one more question sometimes values[5] has no data. It still creates the scn object.
Can we make scns = null when there is no data in values[5].
I basically want to ignore scns when I do serialization using Json.NET
Can we make scns = null when there is no data in values[5].
I basically want to ignore scns when I do serialization using Json.NET
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did the change I get ScnCodes as count=0 . I will need the value as null.
Verify that you have the ternary operator syntaxed properly and in the correct location.
Check that there is in fact nothing in values[5] (you may need to run a debug session for this)
Finally, ensure that you do not have anything else, anywhere creating a list of codes if the list in the object is null.
-saige-
Check that there is in fact nothing in values[5] (you may need to run a debug session for this)
Finally, ensure that you do not have anything else, anywhere creating a list of codes if the list in the object is null.
-saige-
ASKER
Thanks correct. There was one more condition in the terenary operator. This is resolved,
Thanks
Thanks
Open in new window
Proof of concept -Open in new window
Produces the following output --saige-