Select - SelectMany - Any - the differences, when should I take what?

Noah Aas 140 Reputation points
2024-04-23T13:58:20.94+00:00

Hello!

Is there a procedure?
1:n relationships

From the data model to an XML file and generate queries from it?
Data model --> XML Serialize --> Save as xml file, read the xml file and make queries.

Where do I have problems? The procedure.
Primary and foreign keys in the data model.

Is there perhaps a wizard where I can create the data model visually and an XML structure is generated?
https://json2csharp.com/code-converters/xml-to-csharp

Based on the XML, there is another wizard that generates the classes.

Customer 1 : n order

What is better. This.

var CustomerWithOrders = from c in Customers
  join from o in c.Orders
    select new { 
           c.Name, 
           p.Description, 
           o.Price 
          }

Or this?

Customers.SelectMany (
      c => c.Orders, 
      (c, o) => 
         new  
         {
            Name = c.Name, 
            Description = o.Description, 
            Price = o.Price
         }
   ) 
Table A                            Table B 
  ID, Column1, Column2               ID, Column1, Column2, Column3
              1:n                    	n:1
                  Table C=AB		 
			      ID  ForeignA, ForeignB, Price
   
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,275 questions
{count} votes

Accepted answer
  1. Michael Taylor 48,576 Reputation points
    2024-04-23T14:33:42.38+00:00

    There seems to be a lot of questions here so I don't know exactly what you're looking for.

    Is there a procedure? 1:n relationships

    I don't understand this question.

    From the data model to an XML file and generate queries from it?

    Yes, it is called LINQ to XML. It is really just using LINQ against the underlying XML model that is created. Refer to the link for how to use it. However XML is a serialization format so ideally you shouldn't be working with it directly. If you already have the data in your database then just use LINQ against your database like normal. The only time you should really worry about the XML is if you get XML from someone and have to process it from there. If you are sending XML to someone then you shouldn't really need to write queries against it.

    Is there perhaps a wizard where I can create the data model visually and an XML structure is generated?

    There are third party tools that can do it but unless your data structure is really large and complex then it is probably just easier, cleaner and more efficient to write them yourself. It is trivial to create a C# model that is attributed for XML serialization.

    What is better. This.

    The first one is probably cleaner to read. Ultimately it is converted to the extension method equivalent. Use whichever one reads better to you. But we're making an assumption about how your objects were populated. If you are using Entity Framework or another ORM library then the first code will join the tables at the database and return the data to you. If that data is not yet loaded then it'll get loaded at that point. In the second block it is using the Orders on the customers that has already been loaded (ignoring lazy loading). If you haven't loaded the orders (using your ORM) then it won't load the data unless you're using lazy loading (which is bad). So from a purely "safe" point of view I'd lean toward the first code.


0 additional answers

Sort by: Most helpful