LINQ (LAMBDA) How do I add another list of objects and add them to my where? W#

1

Good afternoon, I'm doing an application in C # .Net Core, using lambda for database context handling. I have one question, which is as follows: I have a list of objects with these two attributes: Name and StartDate. Something like:

List<Operations> operations = new List<Operations>();
operations.Add(new Operation() { Name: "Exemplo 1", StartDate: "2018-07-02 00:00:00" };
operations.Add(new Operation() { Name: "Exemplo 2", StartDate: "2017-05-01 13:00:00" };

Now I would like to make a lambda in my context, iterating this list with a condition with the "OR" operator. Something that would look like this in SQL:

SELECT
    *
FROM
    contact t
WHERE
    (t.OperationName = "Exemplo 1" AND t.ContactDate > "2018-07-02 00:00:00")
    OR (t.OperationName = "Exemplo 1" AND t.ContactDate > "2017-05-01 13:00:00")

But I can not. The conditions within the parentheses are separated by AND, when they refer to the same object, and separating the objects by OR. I'm using Lambda, something like this:

dbContext.Teste.Where(x => x...);

@EDIT: A Fiddle I did to demonstrate my problem: link

In that Fiddle, I can only do the where with the first obj of operations, but I need to iterate through all objects. Anyone have any ideas how to do this using Lambda?

    
asked by anonymous 02.07.2018 / 18:58

2 answers

0

I solved the problem.

The way a list can be iterated over another, is by using the "Any ()" function of the second list, within the condition of the first one. It fiddle with the problem solved: link

TL; DR: Excerpt from the code that solved the problem:

List<Contact> listaResultado = 
contacts
.Where(
    x => 
        operations.Any(
            y => 
                x.OperationName.Equals(y.Name) 
                && x.ContactDate > y.StartDate
        )
).ToList();
    
19.07.2018 / 17:02
0

To use AND and OR with lambda, use the && and ||

Let's suppose you have the following class:

public class Operation
{
    public string Name { get; set; }
    public DateTime StartDate { get; set; }
}

With this we can create (Simulating database data) a list of operations as follows

List<Operation> operations = new List<Operation>
{
    new Operation() { Name= "Exemplo 1", StartDate= Convert.ToDateTime("2018-07-02 00:00:00") },
    new Operation() { Name = "Exemplo 2", StartDate= Convert.ToDateTime("2017-05-01 13:00:00") },
    new Operation() { Name = "Exemplo 3", StartDate= Convert.ToDateTime("2016-05-01 13:00:00") }
};

Now we will do a search in this list and play the selected data in another list as follows

List<Operation> operationsDois = operations.Where(x => (x.Name == "Exemplo 1" && x.StartDate == Convert.ToDateTime("2018-07-02 00:00:00")) || (x.Name == "Exemplo 2" && x.StartDate == Convert.ToDateTime("2017-05-01 13:00:00"))).ToList();

Here an example searching by date will fetch the last 30 days

List<Operation> operationsTres = operations.Where(x => x.StartDate >= DateTime.Now.AddDays(-30) && x.StartDate <= DateTime.Now).ToList();

With this the second list has the information searched.

When it is done Where it will go through all data checking what to "marry", to play this in a list you can do something like this:

List<Teste> testes = dbContext.Teste.Where(x => (x.Name == "" && x.StartDate == "") || (x.Name == "" && x.StartDate == "")).ToList();
    
02.07.2018 / 19:06