简体   繁体   中英

Lambda Expression Entity Collection

I have an entity collection tied to another entity (navigation property) I will always have one but for some reason it brings it back as a collection. I need to be able to change one property on this object, the DateViewed property.

var test = _personDetail.CurrentPerson.RecentlyViewed;

is there a way to use this new test object in a lambda expression to change the DateViewed property to DateTime.Now

If it is coming back as a collection and it should not be, you need to go into the designer and set the relationship correctly.


Click on the relationship (the line adjoining the two entities) in the designer, open the properties window, and make sure the End --> Multiplicity property is set to 1 (One) for that end of the relationship.

If it is a "Many-to-one" relationship (ie. the other end of the relationship is set to * (Many) ) , you'll also want to open the Mapping Details (View --> Other Windows --> Mapping Details) and make sure Association --> Maps to... is set to the * Many side of the relationship.

Once this is complete, you should be able to set the DateViewed property like this:

_personDetail.CurrentPerson.DateViewed = DateTime.Now;

Note, I am not 100% certain what you are asking - ie which part comes back as collection, and what RecentlyViewed is (did you mean DateViewed as you mention elsewhere? Taking a guess here - let me know if this isn't what you meant.

If you know there will always be one and only one item in the CurrentPerson collection, you can use Single() - ie:

_personDetail.CurrentPerson.Single().DateViewed = DateTime.Now;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM