Assign item to list if CONTAINS - c#

I have a list
public List<OrderLineItemQuestionPrice> QuestionPriceList { get; set; }
public class OrderLineItemQuestionPrice
{
public string DisplayName { get; set; }
public Price Price { get; set; }
public string QuestionCode { get; set; }
}
I have another list
List<OrderLineItemQuestionPrice> promotionItem;
and I want to assign items of QuestionPriceList to promotionItem if QuestionCode contains 'Promotion'.
List<OrderLineItemQuestionPrice> promotionItem = orderLineItemList.QuestionPriceList.Where(p=>p.QuestionCode.Contains("Promotion"))

One problem which I can see is that you don't calling ToList(); at the end. Also if you don't want to check for exact string Promotion you should call ToLower()
List<OrderLineItemQuestionPrice> promotionItem =
orderLineItemList.QuestionPriceList
.Where(p=>p.QuestionCode.ToLower().Contains("Promotion".ToLower()))
.ToList()

Related

Linq RemoveAll for a predicate condition on a list inside the collection

I for the life of me cannot figure out what this LINQ query needs to be. I do fine on standard LINQ queries but in this case I need to check the condition of items in a list within a collection.
The Task:
I need to remove all messages in the collection where their list of recipients does not have all there handles containing a certain string i.e. "thiscompany.com".
My Attempt So Far:
// Remove internal messages
var internalMessages = messages._results
.Where(x => x.recipients.All(y => y.handle.Contains("somecompany.com")));
messages._results = messages._results.Except(internalMessages).ToList();
As far as I understand this approach would work fine I would just need to implement equality checking on the Message object but for certain reasons I am unable to do this. I know there must be a way with LINQ to execute a one liner that removes all messages based on this sub condition of the Recipients List but I just can't figure out where to go from here.
Message Object:
public class Message
{
public _Links _links { get; set; }
public string id { get; set; }
public string type { get; set; }
public bool is_inbound { get; set; }
public float created_at { get; set; }
public string blurb { get; set; }
public Author author { get; set; }
public List<Recipient> recipients { get; set; }
public string body { get; set; }
public string text { get; set; }
public List<Attachment> attachments { get; set; }
public MessageMetadata metadata { get; set; }
}
Recipient Object:
public class Recipient
{
public _Links _links { get; set; }
public string handle { get; set; }
public string role { get; set; }
}
cName is the string you want to exclude. Assuming you will separately take care of the fact that cName could be null or empty or whitespaces, just do:
var allMessages = messages._results;
var externalMessages = allMessages
.Where(message => !message.recipients.All(to => to.handle.Contains(cName)))
.ToList();
This would be simpler:
message._result = messages._results
.Where(x => x.recipients.Any(y => !y.handle.Contains("somecompany.com"))).ToList();
Or
messages._results
.RemoveAll(x => !x.recipients.All(y => y.handle.Contains("somecompany.com")));

Returning all entries in a C# list matching an int

I have a list that has the following:
class GetMyList
{
public int searchInt { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string Retype { get; set; }
public string Time { get; set; }
public string City { get; set; }
public DateTime dateHere { get; set; }
}
This list could have multiple entries for each int (ie the searchInt 1 could have an entry for 4/11/2017 and another for 4/12/2017).
How do I return ALL of the entries that match the searchInt of 1 in my example above? Preferably so I can just access the dateHere field but it would be nice to know the others, if that matters.
You could simply utilize Linq.
var filter = collection.Where(search => search.searchInt == 1);
The Where would in essence match on a predicate, so in this case all items that evaluate to true in my statement, will be returned.

Find a particular element in List<> and fetch whole data of that row if element is found

I have a List<> which contains collection of objects after getting this list of BillSheetDetail I want to find that billWorkDetails[].details_classification =="xyz" and if it is found then fetch all the data of that particular array index of billWorksDetails[] and store it in other array to display.
How can I do this? I am new to C#
public class BillSheetDetail
{
public DateTime creation_date { get; set; }
public string customer_name { get; set; }
public string subject { get; set; }
public decimal tax_rate { get; set; }
public int total_amount { get; set; }
public string special_instruction { get; set; }
public string comment { get; set; }
public List<BillWorkDetail> billWorkDetails { get; set; }
}
[Serializable]
public class BillWorkDetail
{
public string product_name { get; set; }
public decimal quantity { get; set; }
public string unit { get; set; }
public int unit_cost { get; set; }
public int amount { get; set; }
public string remarks { get; set; }
public int row_no { get; set; }
public string details_classifiction { get; set; }
}
You have to combine Enumerable.Where and Any.
List<BillWorkDetail>[] matchingSheetDetails = billSheetDetailList
.Where(sd => sd.billWorkDetails.Any(d => d.details_classifiction == "xyz"))
.Select(sd => sd.billWorkDetails)
.ToArray();
This creates an array of all matching lists. Since your question is unclear, if you actually only want an array of the matching BillWorkDetail objects:
BillWorkDetail[] matchingBillWorkDetails = billSheetDetailList
.SelectMany(sd => sd.billWorkDetails.Where(d => d.details_classifiction == "xyz"))
.ToArray();
SelectMany selects all matching BillWorkDetail out of the List<BillSheetDetail>. Note that both approaches lose the reference to the BillSheetDetail instance from where it came from.
The solution is using the Where clause:
mySheetDetail.billWorkDetails.Where(x => x.details_classification == "xyz").ToList();
Here is a demonstration of the code that is working well: http://ideone.com/s2cUaR
Try this linq method
List<BillWorkDetail> myBillWorkDetails = new Lis<BillWorkDetail>();
myBillWorkDetails = myBillSheetDetail.billWorkDetails.Where(b => b.classifiction == "xyz").ToList();
This code retrieve all BillWorkDetail with classification xyz.

with list of objects updating wrongly

Here is the class
public class CartManage
{
public int prodId { get; set; }
// public string prodId { get; set; }
public int qty { get; set; }
public string color { get; set; }
public string size { get; set; }
public string Name { get; set; }
public string ShortDescription { get; set; }
public string Specification { get; set; }
public double Price { get; set; }
public string skew { get; set; }
public string weight { get; set; }
public string maxqty { get; set; }
public string productimage { get; set; }
public string prd_vendor_id { get; set; }
public string prdDeliveryDays { get; set; }
public bool GiftingEnabled { get; set; }
public int GiftingId { get; set; }
}
I use this to manage cart in my website i.e List < CartManage > prodList_temp. Now when a person increase the quantity of an item in cart. I add another object in the list(I used to just increase the quantity of the object but due to some functionality i have to implement i need separate objects). I did this by doing a linq query on the list and just adding the result to cart.
var session_updating_data = prodList_temp.Where(p => p.skew == reqired_skew);
prodList_temp.Add(session_updating_data.FirstOrDefault());
The problem im having is that after inserting when ever change anyting in one of the copies the changes are reflected in all the copies. for example i have two quantity of item A, i.e the cart contains two objects containing details of item a created as shown above. Now if I change the giftingEnabled property of any one of the objects it gets reflected in both. Now i found a work around for this by creating a new object. but i would like to know why this weird phenomenon is happening for future reference.
var session_updating_data = prodList_temp.Where(p => p.skew == reqired_skew);
prodList_temp.Add(session_updating_data.FirstOrDefault());
You are passing a reference to that object to your list, so any change in the object will be reflected everywhere. Where as when you create a new object, it's a separate instance.

Joining list with comma

i want to join my list with comma to separate each itemlist from a class but it just can't giving me access to that class property
(i'm sorry i'm not good at explaning this, but i hope with this code sample you will understand)
public class CategoryDetail
{
public string id { get; set; }
public string title { get; set; }
public string href { get; set; }
public string type { get; set; }
public string icon { get; set; }
}
public class RootObjectDetail
{
public List<CategoryDetail> categories { get; set; }
public string categoryList
{
get
{
return string.Join<CategoryDetail>(",", categories.ToArray());
}
}
}
so this is my code that i use for retrieving data from json, and what i want is that in my xaml i will bind it into categoryList and it will showing categoryDetail title property that separated by comma
You don't need to specify generic parameter type - it can be inferred from usage. Also you need to project categories to sequence of titles:
public string categoryList
{
get
{
return String.Join(",", categories.Select(c => c.title));
}
}

Categories

Resources