How to Avoid Recreating Object When Using Let with LINQ

ek_ny

Here is my data:

private List<Department> Data
{
    get
    {
        return new List<Department>
        {
            new Department{
                Id = 1, 
                Name = "Tech",
                Employees = new List<Employee>{
                    new Employee{Name = "x", Id = 1 },
                    new Employee{ Name = "y", Id = 2}
                }
            },
            new Department{
                Id = 2,
                Name = "Sales",
                Employees = new List<Employee>{
                    new Employee{Name = "a", Id = 3},
                    new Employee {Name = "b", Id = 4}
                }
            }
        };
    }
}

and here I am getting a list of all employees with their appropriate departments:

List<Employee> employees = (from department in Departments
                       let d = department
                       from e in d.Employees
                       select new Employee{
                            Id = e.Id,
                            Name = e.Name
                            Department = d
                       }).ToList();

What is bothering me is that I have to recreate my Employee object in order to attach the appropriate department to it. Is there a way that I could write my LINQ statement where I don't have to recreate the Employee?

There might be a better way to phrase this question-- so feel free to let me know is there is.

Edit The reason I'm going down this path is that I'm storing my data by serializing my department:

[
    {
        "Id":1,
        "Name":"Sales",
        "Employees":[{"Id":2,"Name":"x"},{"Id":1,"Name":"y"}]
    },
    {
        "Id":2,
        "Name":"Tech",
        "Employees":[{"Id":3,"Name":"d"},{"Id":4,"Name":"f"}]
    }

]
Amy B

It looks like you want to use LINQ to update an instance. This is not the intended use. Use LINQ to query the instances you want to have, and then loop over the results to update. (non-nested) Loops are not evil.

var query = 
  from d in Departments
  from e in d.Employees
  select new { Employee = e, Department = d };

foreach(var x in query)
{
  x.Employee.Department = x.Department;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?

分類Dev

How to avoid coupling when using useReducer?

分類Dev

How would I pivot this object using linq?

分類Dev

How can NSMutableArray add object using let in swift

分類Dev

How to avoid system databases using SMO object in powershell?

分類Dev

How to avoid slow collapse when using bootstrap collapse and div in a table

分類Dev

How to avoid pylint not-an-iterable when using a custom property class

分類Dev

Nodejs: How to avoid nested .then() when using async/await

分類Dev

How to avoid using a var when doing multiple string replacements in a string

分類Dev

Linq let and if

分類Dev

Something similar to "using" that will create an object and call a method on it when done, but let me do what I want in between

分類Dev

How to avoid escalating to MSDTC in linq 2 sql

分類Dev

How to avoid class name in @type while serializing object to JSON using Jackson

分類Dev

Avoid showing UIAlertView when using PFLogInViewController

分類Dev

How to avoid warning about no return expression when using static_assert?

分類Dev

How to avoid "Invalid byte sequence" when looking for link with text using Nokogiri

分類Dev

How to avoid dropping items when using core.async pub/sub?

分類Dev

How do I avoid absolute pathnames in my code when using Git?

分類Dev

How do I return a new object from a mocked repository using a LINQ Expression?

分類Dev

Linq where clause using filter object

分類Dev

Fill object from dataset using grouping in Linq

分類Dev

Java ArrayList, how to ignore a value from the object when using contains()

分類Dev

PHP - How to solve error "using $this when not in object context"?

分類Dev

How to create single class object when using django-celery?

分類Dev

How to get ID from object when using Vue v-for

分類Dev

Linq filter to avoid a loop

分類Dev

How to populate Dictionary using LINQ?

分類Dev

How to avoid duplicate using Mongodb c++

分類Dev

How to avoid using a return statement in a for-in loop?

Related 関連記事

  1. 1

    Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?

  2. 2

    How to avoid coupling when using useReducer?

  3. 3

    How would I pivot this object using linq?

  4. 4

    How can NSMutableArray add object using let in swift

  5. 5

    How to avoid system databases using SMO object in powershell?

  6. 6

    How to avoid slow collapse when using bootstrap collapse and div in a table

  7. 7

    How to avoid pylint not-an-iterable when using a custom property class

  8. 8

    Nodejs: How to avoid nested .then() when using async/await

  9. 9

    How to avoid using a var when doing multiple string replacements in a string

  10. 10

    Linq let and if

  11. 11

    Something similar to "using" that will create an object and call a method on it when done, but let me do what I want in between

  12. 12

    How to avoid escalating to MSDTC in linq 2 sql

  13. 13

    How to avoid class name in @type while serializing object to JSON using Jackson

  14. 14

    Avoid showing UIAlertView when using PFLogInViewController

  15. 15

    How to avoid warning about no return expression when using static_assert?

  16. 16

    How to avoid "Invalid byte sequence" when looking for link with text using Nokogiri

  17. 17

    How to avoid dropping items when using core.async pub/sub?

  18. 18

    How do I avoid absolute pathnames in my code when using Git?

  19. 19

    How do I return a new object from a mocked repository using a LINQ Expression?

  20. 20

    Linq where clause using filter object

  21. 21

    Fill object from dataset using grouping in Linq

  22. 22

    Java ArrayList, how to ignore a value from the object when using contains()

  23. 23

    PHP - How to solve error "using $this when not in object context"?

  24. 24

    How to create single class object when using django-celery?

  25. 25

    How to get ID from object when using Vue v-for

  26. 26

    Linq filter to avoid a loop

  27. 27

    How to populate Dictionary using LINQ?

  28. 28

    How to avoid duplicate using Mongodb c++

  29. 29

    How to avoid using a return statement in a for-in loop?

ホットタグ

アーカイブ