Searching an object in a collection via lambda and does not return true even if it exist [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
https://www.youtube.com/watch?v=K1xrlc32Tmw&list=PLJUoF2h8Z-brW94dTZ-ZIOhjFq90_lt5K&index=9
4:25 adds a new object in the lineCollection if product does not exist in lineCollection, but at 24:25 it shows duplicating orders? Did i misunderstood how it works?
https://github.com/jedjad/GitHubVS2013

Because the products in duplicate values are not same objects. They may have same names, quantity etc, but initializing a class with same values does not mean that it is the same object as the one initialized before. They are like 2 different apples with same color and size.
If you say that 2 products are same whenever the names are same, then implement IEquatable<Product> in Product class.
public bool Equals(Product other)
{
return Name == other.Name;
}

Related

Why does this LINQ not work? Im trying to get All Countries from a class (only countries [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I'm trying to get all Countries from my database for a combo box.
This is my code for it:
public IQueryable<Customer> GetAllCountries()
{
var query = from e in Entities
select e.Country;
return query;
}
It gives me an error at return query;
I only want the Countries for my combo box, even though there are cities and all sorts in the Customer class.
I managed to fix it by using IQueriable<string> instead of Customer!

How to find the name of the person from the list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How to return a person's name when we search for a hobby and it should return empty when there are no matching hobbies.
There are many ways to skin this cat. Using Linq is probably the most standard.
The operation you want to do is to select the Key of the dictionary where the value (the array of hobbies) contains the string "Yoga"
val keys = hobbies.Where(keyvalue => keyvalue.contains("Yoga")).Select(keyvalue => keyvalue.key);
now you have a sequence of keys where one of the hobbies is Yoga.
If there is only a single one, and it's an error if there are more, get CC with keys.Single().
If there could be zero or more, and you just want an arbitrary one, you want keys.FirstOrDefault() which returns null if there isn't one.
In between you have SingleOrDefault() for 0 or 1, or First() for 1 or more select an arbitrary one.

How to check array for a value in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to help someone out and this isn't my area of expertise so thought maybe someone can help me help someone else.
I have a field called Master that contains an array. I also have a field called Original that contains a string. I want to check if the string in Original is in the array field called Field1 and then with an if statement do something if true / false
"Original":"1234",
"Master":[{"ID":1,"Field1":12345},
{"ID":2,"Field1":123456},
{"ID":3,"Field1":1234},
{"ID":4,"Field1":12344}]
The array can be different each time and have a different amount of records in it.
Can anyone help me?
if Original and Master would be properties of the same class and in a variable named instance then you could use Linq to do:
bool isPresent = instance.Master.Any(entry => entry.Field1 == instance.Original);
Obviously you would need to first serialize the json to an instance of this class.

Object oriented c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a class called Promotion (model class). I want a promotion to have a type called Category where Category is its own model class I'm assuming where I would define all the types of categories such as fast food foot wear jewelry and so on. I'm not quite sure of how to go about this though so for example, my class called category is already a set thing but my class promotion is something where before I create it I need to set it with a category with the viable category options. thank you!
why don't you create a property called Categories of type Category in the Promotion?
enum Algorithms
{
FCFS,
SJF,
PRIORITY,
RR
}
class Process
{
public Algorithm algorithms{get;}
}
For the detailed difference please read the answer from this SO answer

Accessing object item c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an object passed into a function. Now I want to access the object item like we have in the class function (EX: obj a: a.name):
I want to access level obejct slaType item.
"level.slaType"
You need to cast level object to that particular class type..
LevelType levelObject = level as LevelType;
if(levelObject != null)
{
levelObject.slaType;
}

Categories