I am working on dynamic query building in LINQ using Expression Tree.
I have taken the reference to the following post
https://www.codeproject.com/Tips/582450/Build-Where-Clause-Dynamically-in-Linq
How can I build expression if I want to check all the element in the list contains in another collection or not?
I have a Person class
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
and I have a list
List<Person> personList = new List<Person>()
{
new Person{ Name = "Shekhar", Age = 31},
new Person{ Name = "Sandip", Age = 32},
new Person{ Name = "Pramod", Age = 32},
new Person{ Name = "Kunal", Age = 33}
};
I have another list
List<string> nameList = new List<string>() { "Sandip", "Prashant" };
How can I build expression tree to check all the element in the list "nameList" contains in "personList" and give result true or false?
try this:
public bool Find(List<string> nameList, List<Person> personList)
{
foreach (var name in nameList)
if (personList.FirstOrDefault(person => person.Name == name) != null)
{
// Find
return true;
}
return false;
}
try this:
bool contained = !personList.Select(l=>l.Name).Except(nameList).Any();
Related
This question already has answers here:
string to variable name
(4 answers)
Closed 9 years ago.
I am reading the name of a string variable from the database (e.g. "_datafile"). I want to know how I can access a named variable within my program using this string.
I have already tried using a dictionary, hash table, and a switch-case statement but I would like to have the variable resolve itself dynamically. Is this possible?
Do you mean you want to get the value of a field using the field name as a string?
public class MyClass
{
public string _datafile;
public MyClass()
{
_datafile = "Hello";
}
public void PrintField()
{
var result = this.GetType().GetField("_datafile").GetValue(this);
Console.WriteLine(result); // will print Hello
}
}
EDIT: #Rick, to respond to your comment:
public class MyClass
{
public IEnumerable<string> _parameters = new[] { "Val1", "Val2", "Val3" };
public void PrintField()
{
var parameters = this.GetType().GetField("_parameters").GetValue(this) as IEnumerable;
// Prints:
// Val1
// Val2
// Val3
foreach(var item in parameters)
{
Console.WriteLine(item);
}
}
}
If you want to get the value of a field based on its string name you will have to use reflection.
class MyClass
{
public int DataFile { get; set; }
public int _datafile;
}
var ob = new MyClass();
var typ = typeof(MyClass);
var f = typ.GetField("_datafile");
var prop = typ.GetProperty("DataFile");
var val = f.GetValue(ob);
var propVal = prop.GetValue(ob);
Usually you would create a class representing the values of one table record. If your table has an ID a FirstName and a LastName column, you would create a class like this
public class Person
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Then you create a list of persons
var people = new List<Person>();
Now you can add persons to the list.
var p = new Person();
p.ID = 5;
p.FirstName = "John";
p.LastName = "Doe";
people.Add(p);
You can use a DataReader in order to read from a table
string sql = "SELECT * FROM tblPerson WHERE LastName LIKE #pattern";
cmd = new SqlCommand(sql);
cmd.Connection = "server=test;uid=sa;pwd=manager;database=northwind";
cmd.Parameters.AddWithValue("#pattern", "A%"); // Names beginning with "A"
using (SqlDataReader reader = cmd.ExecuteReader()) {
// Get column indexes
int idOrdinal = reader.GetOrdinal("ID");
int firstNameOrdinal = reader.GetOrdinal("FirstName");
int lastNameOrdinal = reader.GetOrdinal("LastName");
while(reader.Read()) {
var p = new Person();
p.ID = reader.GetInt32(idOrdinal);
p.FirstName = reader.GetString(firstNameOrdinal);
p.LastName = reader.GetString(lastNameOrdinal);
people.Add(p);
}
}
I have been digging this quite a while.
public class Person
{
public string Name { get; set; }
public string Age { get; set; }
public List<Person> Children { get; set; }
}
I want a single LINQ query to find out "All the persons whose Age > 4 in this collection".
Note: You have to traverse Collection of Person + Collection of Children, so each children object will have a collection of Person till Children becomes null.
First i can't understand why all your properties private and Age is not int type. So my class looks like this:
public partial class Person
{
public string Name { get; set; }
public int Age { get; set; }
public List<Person> Childrens { get; set; }
}
Note partial word. This word will allow you to place your class logic in separate files and this could be usefull when you creating some custom logic in class.
Then I simply create this method in different file:
public partial class Person
{
public Person GetPersonWithChindren(int maxAge)
{
return new Person
{
Age = this.Age,
Name = this.Name,
Childrens = this.Childrens != null
? this.Childrens
.Where(x => x.Age < maxAge)
.Select(x => x.GetPersonWithChindren(maxAge)) //this line do recursive magic
.ToList()
: null
};
}
}
As you can see this method checking Age of each child and if Age is ok then it checks next level of hierarchy untill Childrens is null.
So you can use it like this:
var person = new Person()
{
//initialisation of your collection here
}
//result will contains only nodes where Person have age < 4 and Childs that have age < 4
var result = person.GetPersonWithChindren(4);
Note that this solution will work normal with linqToEntities. But if you using LinqToSQL this expression produces query to DB on each Person entity. So if you have many Persons and deep hierarhy it will costs you a lot of machine time. In that case you should to write stored procedure with CTE instead of LinQ query.
UPDATE:
You even can write more general solution with a help of Func<T> class like this:
public partial class Person
{
public Person GetPersonWithChindren(Func<Person, bool> func)
{
return new Person
{
Age = this.Age,
Name = this.Name,
Childrens = this.Childrens != null
? this.Childrens
.Where(x => func(x))
.Select(x => x.GetPersonWithChindren(func))
.ToList()
: null
};
}
}
And then you can use it like this:
var result = person.GetPersonWithChindren(x => x.Age < 4);
You always can change your criteria now where you want to use your function.
Create a visitor. In this example by implementing a helper class:
public static class Helpers
public static IEnumerable<Person> GetDescendants(this Person person)
{
foreach (var child in person.Children)
{
yield return child;
foreach (var descendant in child.GetDescendants())
{
yield return descendant;
}
}
}
this is one of the times where the "yield return many" would be useful.
If you're ensuring that .Children is automatically created, then this works:
Func<Person, Func<Person, bool>, Person> clone = null;
clone = (p, f) => f(p) ? new Person()
{
Name = p.Name,
Age = p.Age,
Children = p.Children.Select(c => clone(c, f)).Where(x => x != null).ToList(),
} : null;
var olderThan4 = clone(person, p => p.Age > 4);
Yes, that's it. Effectively three lines.
If you start with this data:
var person = new Person()
{
Name = "Fred", Age = 30,
Children = new List<Person>()
{
new Person() { Name = "Bob", Age = 7, },
new Person() { Name = "Sally", Age = 3, }
},
};
...then you get this result:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
private List<Person> _children = null;
public List<Person> Children
{
get
{
if (_children == null)
{
_children = new List<Person>();
}
return _children;
}
set
{
_children = value;
}
}
}
If the tags didn't give it away, I'm working with C#'s XmlSerializer class.
Say, for example, I have a Person class with various properties including age (int), name (string), and deceased (bool). Is there a way to specify that I don't want to serialize any objects whose deceased flags are true?
Edit: I should have specified, but unfortunately due to the situation I can't really edit my list of objects because it's a member of another class, which is what I'm actually serializing. Are there any other suggestions?
Assuming that you have following type of Class structure(As you specified in the comment)
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
public bool Deceased { get; set; }
}
public class Being
{
public string Data { get; set; }
[XmlElement("Human")]
public Person Human { get; set; }
public bool ShouldSerializeHuman()
{
return !this.Human.Deceased;
}
}
Here I have added a method called ShouldSerialize this is called a pattern for XML serialization. Here you can use XmlArray and XmlArrayItem for lists etc.(With given name) then the ShouldSerialize checks if it can be serialized.
Below is the code I used for testing.
private static void Main(string[] args)
{
var livingHuman = new Person() { Age = 1, Name = "John Doe", Deceased = true };
var deadHuman = new Person() { Age = 1, Name = "John Doe", Deceased = false };
XmlSerializer serializer = new XmlSerializer(typeof(Being));
serializer.Serialize(Console.Out, new Being { Human = livingHuman, Data = "new" });
serializer.Serialize(Console.Out, new Being { Human = deadHuman, Data = "old" });
}
And here's the output:
=============================
Update:
If you have list of Person as Humans:
public class Being
{
// [XmlAttribute]
public string Data { get; set; }
// Here add the following attributes to the property
[XmlArray("Humans")]
[XmlArrayItem("Human")]
public List<Person> Humans { get; set; }
public bool ShouldSerializeHumans()
{
this.Humans = this.Humans.Where(x => !x.Deceased).ToList();
return true;
}
}
Sample Test:
private static void Main(string[] args)
{
var livingHuman = new Person() { Age = 1, Name = "John Doe", Deceased = true };
var deadHuman = new Person() { Age = 1, Name = "John Doe", Deceased = false };
var humans = new List<Person> { livingHuman, deadHuman };
XmlSerializer serializer = new XmlSerializer(typeof(Being));
serializer.Serialize(Console.Out, new Being() { Humans = humans, Data = "some other data" });
}
Output:
If you have a list of Person objects and only want to serialise some of them, then just filter out the ones you don't need. For example:
List<Person> people = GetPeople(); //from somewhere
List<Person> filteredPeople = people.Where(p => !p.Deceased);
Now you only need to serialise filteredPeople.
This should be a fundamental C# question. Suppose I have a Person class which contains a property named Pets which is a List<Pet>.
If I want to update a pet, I can get the pet as a variable and manipulate properties on it, but I can't seem to create a new pet object and assign it to the existing pet object. I get a "Value assigned is not used in any execution path" warning. I created some very simple code that outlines this issue.
In my real code, I want to be able to use a new child object and replace and existing object. If you can show me how I can do that with the sample updatedCat below, I would greatly appreciate it!
class Program
{
static void Main(string[] args)
{
var program = new Program();
program.RunMe();
}
public void RunMe()
{
var myPerson = new Person() { Name = "John Doe" };
var dog = new Pet() { Type = "Dog", Name = "Woofie" };
var cat = new Pet() { Type = "Cat", Name = "Chester" };
myPerson.Pets.Add(dog);
myPerson.Pets.Add(cat);
Console.WriteLine("Initial Pet Status:");
ListPets(myPerson);
var currentDog = myPerson.Pets.SingleOrDefault(p => p.Type == "Dog");
currentDog.Name = "Snoopie";
Console.WriteLine("\r\nPet Status After Updating Dog Directly (name should be 'Snoopie'):");
ListPets(myPerson);
var updatedCat = new Pet() { Type = "Cat", Name = "Felix" };
var currentCat = myPerson.Pets.SingleOrDefault(p => p.Type == "Cat");
currentCat = updatedCat;
//Resharper shows "Value assigned is not used in any execution path" for the currentCat
//and the current cat is never updated
Console.WriteLine("\r\nPet Status After Trying to Update Cat by Assigning a New Cat to existing Cat (name should be 'Felix' but it's not):");
ListPets(myPerson);
Console.ReadLine();
}
public void ListPets(Person person)
{
foreach (var pet in person.Pets)
{
Console.WriteLine(string.Format(" {0} has a {1} named {2}", person.Name, pet.Type, pet.Name));
}
}
}
public class Person
{
public string Name { get; set; }
public List<Pet> Pets { get; set; }
public Person()
{
Pets = new List<Pet>();
}
}
public class Pet
{
public string Type { get; set; }
public string Name { get; set; }
}
Edit to add Id value to Pet and create a InsertOrUpdatePet method
(Note: I removed the Console.Writeline command for succinctness)
class Program
{
static void Main(string[] args)
{
var program = new Program();
program.RunMe();
}
public void RunMe()
{
var myPerson = new Person() { Name = "John Doe" };
var dog = new Pet() { Id = 1, Type = "Dog", Name = "Woofie" };
var cat = new Pet() { Id = 2, Type = "Cat", Name = "Chester" };
myPerson.Pets.Add(dog);
myPerson.Pets.Add(cat);
var updatedCat = new Pet() { Id = 2, Type = "Cat", Name = "Felix" };
InsertOrUpdatePet(myPerson, updatedCat);
var currentCat = myPerson.Pets.SingleOrDefault(p => p.Type == "Cat");
}
public void InsertOrUpdatePet(Person person, Pet pet)
{
var currentPet = person.Pets.SingleOrDefault(p => p.Id == pet.Id);
if(currentPet == null)
{
person.Pets.Add(pet);
}
else
{
currentPet = pet; // This doesn't work
person.Pets.SingleOrDefault(p => p.Id == pet.Id) = pet; //This throws an error
}
}
}
public class Person
{
public string Name { get; set; }
public List<Pet> Pets { get; set; }
public Person()
{
Pets = new List<Pet>();
}
}
public class Pet
{
public int Id { get; set; }
public string Type { get; set; }
public string Name { get; set; }
}
That depends on what do you want to do conceptually. If you want to say that the person got rid of the old cat and has now a new cat, you should do that: remove the old cat and then the new cat:
var updatedCat = new Pet() { Type = "Cat", Name = "Felix" };
var currentCat = myPerson.Pets.SingleOrDefault(p => p.Type == "Cat");
myPerson.Remove(currentCat);
myPerson.Add(updatedCat);
If, on the other hand, the person still has the same cat, but the cat was renamed, you should rename the old cat, the same way you did it with the dog:
var currentCat = myPerson.Pets.SingleOrDefault(p => p.Type == "Cat");
currentCat.Name = "Felix";
Your code doesn't work, because currentCat is something that references the cat, not the spot in the list. If you want some way to represent the spot in the list, you could use indexing into the list, as Jason suggested.
You'll need to Remove the pet you want to replace and Add a new Pet.
Alteratively, you can say
person.Pets[index] = new Pet() { Type = "Cat", Name = "Felix" };
where index is the index of the Pet that you want to replace.
What you're not understanding is that a List of Pet is merely a list of references to Pet instances.
Getting a reference to one of these Pet instances and storing it in a variable and assigning a reference to a new instance of Pet does not change where any of the references in the List refer to. Of course not, you haven't modified the list in anyway. That's why you either have to remove the element you want to replace, and add a new element, or directly assign a reference to a new instance to the element that you want to replace.
I have the following models:
public class Person
{
long Id;
string name;
}
public class Student : Person
{
string studentId;
}
public class Bus
{
long Id;
public ICollection<Person> riders {set; get;}
}
public class SchoolBus : Bus
{
long schoolBusNumber;
}
I also have the following code:
SchoolBus schoolBus = new SchoolBus();
schoolBus.riders = new List<Person>
{
new Student { name = "Jim" },
new Student { name = "Jane }
}
var query = from rider in SchoolBus.riders
select new
{
(rider as Student).studentId;
}
Students and Person are set up as separate tables and I'm using DbContext.
I know why this would not work, but what are possible solutions for me to get this to return the right studentId by using a Person collection?
try this:
var studentIds = rider.OfType<Student>().Select(x => x.studentId);
If your code is exactly what you shown, this will work:
SchoolBus schoolBus = new SchoolBus();
schoolBus.riders = new List<Person>
{
new Student { name = "Jim" },
new Student { name = "Jane }
}
var query = from rider in SchoolBus.riders
select new
{
riderID = (rider as Student).studentId;
}
But if your query runs on linq2entity, you should show your exact code, and your problem.