开发者

Need help writing an anonymous method?

Forgive me if my question is technically worded wrong but I basically need an anonymous method or a Func delegate to encapsulate the following functionality:

                if (Cache.CurrentCustomer == null)
                {
    开发者_如何学Go                return null;
                }
                else
                {
                    return Cache.CurrentCustomer.PersonID; // (Guid type)
                }

The above if statement will return a value that is going to be assigned against an Order entity that has a PersonID property field exposed that accepts a nullable guid type.

If a Func delegate is possible then can instantiate on the fly like:

orderToInsert.PersonID = new Func() => { ... }

I would typically ship my if statement scenario out into a help support method this is a good opportunity to learn something I have been trying to pick for ages! TIA!!!


Here it is in lambda form:

Func<Guid?> lambda = () => Cache.CurrentCustomer == null 
                             ? (Guid?)null 
                             : Cache.CurrentCustomer.PersonID;

You would then use it as in

orderToInsert.PersonID = lambda();

Update: If you are only trying to see what's possible here, then you can also do this:

orderToInsert.PersonID = (() => Cache.CurrentCustomer == null 
                             ? (Guid?)null 
                             : Cache.CurrentCustomer.PersonID)();

which is really just a roundabout way of doing the classic:

orderToInsert.PersonID = Cache.CurrentCustomer == null 
                             ? (Guid?)null 
                             : Cache.CurrentCustomer.PersonID;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜