none
LINQ to create a calculated value based on the value of two other columns RRS feed

  • Question

  • Simplified scenario:

    I want to create a calculated value based on the value of two properties, Field1 and Field2. When Field1 has a value, Field2 is null, and viceversa. They can't be both null.

    Let's call the calculated value "cv". The rule is: If Field1 is null, cv = Field2, and if Field2 is null, cv = Field1.

    How can I achieve this using LINQ to Entities?

    thanks.

    Tuesday, March 11, 2014 6:01 PM

Answers

  • Hi Nicolás;

    Just use the null-coalescing operator in your select statement as shown below.

    var result = from o in DtatContext.MyTable
                 select o.Field1 ?? o.Field2;

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, March 11, 2014 6:42 PM

All replies

  • Does this help as start ?

            DataTable objTable = new DataTable();
            objTable.Columns.Add("field1", typeof(int)); objTable.Columns.Add("field2", typeof(int));
        
            objTable.Rows.Add(new object[] { 11, null });
            objTable.Rows.Add(new object[] { 2, null });
            objTable.Rows.Add(new object[] { null, 3 });
            objTable.Rows.Add(new object[] { null, 41 });
    
            objTable.AcceptChanges();
           
            var result = objTable.AsEnumerable().Sum(x =>  (x.Field<int?>("field1") == null ? x.Field<int?>("field2") : x.Field<int?>("field1")));
    
            Console.WriteLine(result);

    Tuesday, March 11, 2014 6:35 PM
  • Hi Nicolás;

    Just use the null-coalescing operator in your select statement as shown below.

    var result = from o in DtatContext.MyTable
                 select o.Field1 ?? o.Field2;

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, March 11, 2014 6:42 PM
  • Hi Nicolás;

    Just use the null-coalescing operator in your select statement as shown below.

    var result = from o in DtatContext.MyTable
                 select o.Field1 ?? o.Field2;

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Gracias Fernando, no conocía ese operador.
    Tuesday, March 11, 2014 10:59 PM
  •   Su muy a gusto, contento de haber podido ayudar.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, March 12, 2014 3:12 AM