?: Operator in LINQ Query

后端 未结 5 2128
时光取名叫无心
时光取名叫无心 2021-02-20 15:20
  • How do I utilize a ?: operator in the SELECT clause of a LINQ query? If this can\'t be done, how can I emulate one? The goal is to get a CASE block in my select claus

5条回答
  •  醉梦人生
    2021-02-20 16:25

    I'm fairly new to Linq to SQL but I'm pretty sure it would go like this:

    var query =
        from a in db.tblActivities
        from i in a.tblIPs
        from u in i.tblUsers 
        select new
        {
            userName = (u.UserName == null)
                ? i.Address
                : u.UserName,
            a.Request,
            a.DateTime
        };
    

    The if statement needs to be in parentheses and the results outside of them. As for the joins, you follow the chain down from one->many.

提交回复
热议问题