将一些属性和一些逻辑混合在一起的列表
我需要使用对象列表中的一个或两个属性将字符串列表混搭在一起,但依赖于一点逻辑。列表中的每个项目应该是属性 1 和属性 2 连接在一起的字符串值,并用连字符分隔。除非属性 1 与属性 2 具有相同的字符串值,在这种情况下,该项目将只是属性 1。为了澄清,所有对象都是相同的类型,并且这两个属性都是字符串值。
这里有一个例子来说明它,以防万一我没有理解。
Object 1: Prop 1 = "Object 1", Prop 2 = "Object 1" // Same value
Object 2: Prop 1 = "Object 2", Prop 2 = "Obj 2" // Different
结果列表=
[0] = "Object 1",
[1] = "Object 2-Obj 2"
有没有一种“简洁”的方法可以使用一些 Linq 或其他东西来做到这一点,而不是手动循环?
I need to mash a list of strings together using one or two properties from a list of objects, but dependant on a bit of logic. Each item in the list should be the string value of property 1 and property 2 joined together, separated by a hyphen. UNLESS property 1 has the same string value as property 2, in which case the item will simply be property 1. Just to clarify, all the objects are the same type and the two properties are both string values.
Here's an example to illustrate it just in case I didn't make sense..
Object 1: Prop 1 = "Object 1", Prop 2 = "Object 1" // Same value
Object 2: Prop 1 = "Object 2", Prop 2 = "Obj 2" // Different
Resulting list =
[0] = "Object 1",
[1] = "Object 2-Obj 2"
Is there a "neat" way of doing that using a bit of Linq or something rather than looping through manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西?
something like this?