Reputation: 11662
I have to search field in which users can enter Order Number
or keyword
. keyword needs to search in MemberOrderLineItem
ItemName
value.
right now I am doing for Order number like this.
memberOrderInformationList = memberOrderInformationList
.Where(x => x.OrderNumber.Contains(searchString))
.OrderByDescending(x => x.OrderDate)
.ToList();
But how to include MemberOrderLineItem ItemName
value also?
public class MemberOrderInformation
{
public string OrderNumber { get; set; }
public DateTime? OrderDate { get; set; }
...
public List<MemberOrderLineItem> LineItems { get; set; }
public MemberOrderInformation()
{
LineItems = new List<MemberOrderLineItem>();
}
}
[Serializable]
public class MemberOrderLineItem
{
public string OrderNumber { get; set; }
public int OrderLineNumber { get; set; }
public string ItemName { get; set; }
public string Status { get; set; }
...
...
}
Upvotes: 0
Views: 126
Reputation: 6232
memberOrderInformationList = memberOrderInformationList
.Where(x => x.OrderNumber.Contains(searchString))
.OrderByDescending(x => x.OrderDate)
.Where(x=>x.LineItems.Any(x=>x.ItemName == "hej"))
.ToList();
Upvotes: -1
Reputation: 39376
You could try this:
memberOrderInformationList = memberOrderInformationList.Where(x=>x.OrderNumber.Contains(searchString)
|| x.LineItems.Any(y=>y.ItemName.Contains(keyword)))
.OrderByDescending(x => x.OrderDate)
.ToList();
Upvotes: 0
Reputation: 1560
if I am understanding the question correctly, you can try
memberOrderInformationList =
memberOrderInformationList.Where(x=>x.OrderNumber.Contains(searchString)
|| x.LineItems.FirstOrDefault(l => l.ItemName.Contains(searchString)) != null)
.OrderByDescending(x => x.OrderDate).ToList();
EDIT: Looking at the other answers, using .Any()
opposed to .FirstOrDefault
is a cleaner option, though both should work equally well.
Upvotes: 0
Reputation: 1964
try this
memberOrderInformationList = memberOrderInformationList.Where(x=>x.OrderNumber.Contains(searchString)
|| x.LineItems.any(p => p.ItemName.Contains(searchString))
).OrderByDescending(x => x.OrderDate).ToList();
Upvotes: 2