Getting Error : Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access. RRS feed

  • Question

  • I try to left join two list and update first list's one property value with the value from second list's property but getting error Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

    This code is not giving any error

                var list1 = new List<ItemOne>
                                new ItemOne {IDItem = 1, OneProperty = "1"},
                                new ItemOne {IDItem = 2, OneProperty = null},
                                new ItemOne {IDItem = 3, OneProperty = "3"},
                                new ItemOne {IDItem = 4, OneProperty = "4"}
                var list2 = new List<ItemTwo>
                                new ItemTwo {IDItem = 2, TwoProperty = "2"},
                                new ItemTwo {IDItem = 3, TwoProperty = "3"},
                var query = list1.Join(list2, l1 => l1.IDItem, l2 => l2.IDItem, (l1, l2) =>
                    l1.OneProperty = l2.TwoProperty;
                    return l1;

    But this below code is throwing error.......what i am missing ?

                var data = (from a in list1
                           join b in list2
                           on a.IDItem equals b.IDItem into c
                           from d in c.DefaultIfEmpty()
                           select new
                               a.OneProperty=(d==null ? string.Empty : d.TwoProperty)

    class ItemOne
            public int IDItem { get; set; }
            public string OneProperty { get; set; }
        class ItemTwo
            public int IDItem { get; set; }
            public string TwoProperty { get; set; }

    please guide me and rectify my code. thanks

    Monday, April 8, 2019 7:59 PM


  • The error is clarifying what is wrong. 

    " Invalid anonymous type member declarator."

    You have an anonymous type where you are declaring the member incorrectly. Given the code you posted the only place I see an anonymous type is in your select new line. On that line you have a left side property called `a.OneProperty` but property names cannot have a dot in them so this is invalid. You need to simplify the property name so it can be a property on the anonymous type you're creating. 

    select new 
       MyProperty = (d == null ...

    Note that this code isn't equivalent to the code you said was working. In the code you had that was working you're returning back the first item (`a` in this case) after you've set its IDProperty to whatever the second item's property is. But, of course, that won't work in LINQ because LINQ is for querying, not modifying data. If you want to stick with the LINQ syntax then you'll need to return an anonymous type with the first item along with the second item's IDProperty. Then separately you can iterate the results and assign the first item's property value to the second. Alternatively you can return a brand new item of the same type as the first (left list) but with all the values from the first item except the IDProperty. If your type is small then this is how I'd good.

    select new ItemOne {
       IDItem = a.IDItem,
       OneProperty = d?.TwoProperty ?? A.OneProperty
    Note: didn't test this code to see if it would compile.

    Michael Taylor

    Monday, April 8, 2019 9:11 PM