Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set properties on all items from a linq query with values from another object that is also pulled from a query?

I have a query pulling from a database:

List<myClass> items = new List<myClass>(from i in context
                      select new myClass
                      {
                          A = i.A,
                          B = "",    // i doesn't know this, this comes from elsewhere
                          C = i.C
                      }

I also have another query doing a similar thing:

List<myClass2> otherItems = new List<myClass2>(from j in context
                            select new myClass2
                            {
                                A = j.A,   // A is the intersection, there will only be 1 A here but many A's in items
                                B = j.B
                            }

In reality these classes are much larger and query data that is separated not only by database but by server as well. Is it possible to use a LINQ query to populate the property B for all items where items.A intersect? All of the built in LINQ predicates appear only to do aggregates, selections or bool expressions.

In my brain I had something like this, but this is all off:

items.Where(x => x.B = (otherItems.Where(z => z.A == x.A).Single().B));

Or am I being ridiculous with trying to make this work in LINQ and should just abandon it in favor of a for loop where the actual setting becomes trivial? Because of deadlines I will be resorting to the for loop (and it's probably going to end up being a lot more readable in the long run anyway), but is it possible to do this? Would an extension method be necessary to add a special predicate to allow this?

like image 998
Joel Etherton Avatar asked Oct 21 '11 15:10

Joel Etherton


People also ask

Can we use multiple where clause in LINQ?

Well, you can just put multiple "where" clauses in directly, but I don't think you want to. Multiple "where" clauses ends up with a more restrictive filter - I think you want a less restrictive one.

How does a LINQ query transform to a SQL query?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.

What is SelectMany in LINQ C#?

The SelectMany in LINQ is used to project each element of a sequence to an IEnumerable<T> and then flatten the resulting sequences into one sequence. That means the SelectMany operator combines the records from a sequence of results and then converts it into one result.

What is difference between select and SelectMany in LINQ?

Select and SelectMany are projection operators. A select operator is used to select value from a collection and SelectMany operator is used to selecting values from a collection of collection i.e. nested collection.


1 Answers

LINQ is designed for querying. If you're trying to set things, you should definitely use a loop (probably foreach). That doesn't mean you won't be able to use LINQ as part of that loop, but you shouldn't be trying to apply a side-effect within LINQ itself.

like image 113
Jon Skeet Avatar answered Oct 07 '22 05:10

Jon Skeet