Reputation: 19903
I have a collection of this object :
public Class MyObject()
{
public int Id { get; set; }
public int Code { get; set; }
public string NL { get; set; }
public string FR { get; set; }
public string Value { get; set; }
}
I have this
IList<MyObject> listObject = new List<Object>();
bool res = MyMethod();
I'd like depending of the res result copy all the value of FR to Value and from NL to Value if res is false :
I try this :
var res = (from p in listObject select new { Id = p.Id, Value = ?????? });
the ?????? reprsent the code I don't find :(
Any idea ?
Thanks,
UPDATE1 I made a more generic method :
public void MyTest<T>(IList<T> list) where T : ILookup
{
bool res = MyMethod();
var result = (from p in list select new { Id = p.Id, Value = res? p.FR : p.NL });
return result;
}
What is the return type ?
Upvotes: 2
Views: 3038
Reputation: 16984
A conditional or ternary operator will help here. An example of how it work is:
var result = booleanExpression ? valueIfTrue : valueIfFalse;
applied to your code it would look something like this:
var copy = (from p in listObject select new { Id = p.Id, Value = res ? p.FR : p.NL});
If you wish the result to be returnable from the method you will have to use a class with your select statement rather than createing an anonymous type:
var copy = (from p in listObject select new MyObject { Id = p.Id, Value = res ? p.FR : p.NL});
where MyObject has properties defined as Id and Value. You could then return it as
IEnumerable<MyObject >.
Upvotes: 5
Reputation: 886
Use the ternary operator
var result = (from p in listObject select new { Id = p.Id, Value = res ? p.FR : p.NL });
Upvotes: 4