I'm trying to learn Ienumerable with linq.
What I want to do is to print the userlist after filtering it with linq. But I am encountering such a console screen.
Main Code:
class Program
{
static void Main(string[] args)
{
LinqExtensions<UserObject> linq = new LinqExtensions<UserObject>();
var users = new List<UserObject>()
{
new UserObject()
{
Id=1,
FirstName="xx",
LastName="yy",
EmailAdress="[email protected]",
CreateDateTime=DateTime.Now
},
new UserObject()
{
Id=2,
FirstName="aa",
LastName="bb",
EmailAdress="[email protected]",
CreateDateTime=DateTime.Now
}
};
var userList = linq.Where_EqualsEvenID(users);
Console.WriteLine(userList);
Console.ReadLine();
}
}
And Extensions Code:
public class LinqExtensions <T> where T:UserObject
{
public IEnumerable<T> Where_EqualsEvenID(IEnumerable<T> TTypeIEnum)
{
return TTypeIEnum.Where(p => p.Id % 2 != 0).ToList();
}
public IEnumerable<T> Where_OddEvenID(IEnumerable<T> TTypeIEnum)
{
return TTypeIEnum.Where(p => p.Id % 2 != 0).ToList();
}
public IEnumerable<T> Where_EqualsId(IEnumerable<T> TTypeIEnum,int id)
{
return TTypeIEnum.Where(p=>p.Id==id).ToList();
}
}
Console Message: System.Collections.Generic.List`1[Linq_1.Objects.UserObject]
Help me please, thanks a lot.
CodePudding user response:
Why are you getting the type text instead of values?
Console.WriteLine(Object)
writes the text representation of an object.- To represent the object into text, it calls
.ToString()
method. - If it is not overridden in your class, then it invokes
.ToString()
default implementation. - This default implementation is nothing but returning the string representation of the current object
How to fix your issue?
To fix this issue, you override ToString()
within the UserObject
class.
public class UserObject
{
...
public override string ToString()
{
//You can include fields as per your need.
return $"{this.FirstName} {this.LastName}";
}
}
Now while printing the list of UserObject
, you need to iterate over each object of UserObject
and print the details.
foreach(var userObj in userList)
Console.WriteLine(userObj);
You can also use string.Join()
,
Console.WriteLine(string.Join(Environment.NewLine, userList));
This will print
xx yy
aa bb
CodePudding user response:
You have to loop IEnumerable collection and use Json serialized(add Newtonsoft.Json extension to your project) to convert and print object in console.
Check below code
using Newtonsoft.Json;
namespace SampleExample
{
internal class Program
{
static void Main(string[] args)
{
LinqExtensions<UserObject> linq = new LinqExtensions<UserObject>();
var users = new List<UserObject>()
{
new UserObject()
{
Id=1,
FirstName="xx",
LastName="yy",
EmailAdress="[email protected]",
CreateDateTime=DateTime.Now
},
new UserObject()
{
Id=2,
FirstName="aa",
LastName="bb",
EmailAdress="[email protected]",
CreateDateTime=DateTime.Now
},
new UserObject()
{
Id=3,
FirstName="zak",
LastName="tos",
EmailAdress="[email protected]",
CreateDateTime=DateTime.Now
},
new UserObject()
{
Id=4,
FirstName="Michael",
LastName="J",
EmailAdress="[email protected]",
CreateDateTime=DateTime.Now
}
};
var userList = linq.Where_EqualsEvenID(users);
foreach (var item in userList)
{
Console.WriteLine(JsonConvert.SerializeObject(item, Formatting.Indented));
}
Console.ReadLine();
}
public class LinqExtensions<T> where T : UserObject
{
public IEnumerable<T> Where_EqualsEvenID(IEnumerable<T> TTypeIEnum)
{
return TTypeIEnum.Where(p => p.Id % 2 == 0).ToList();
}
public IEnumerable<T> Where_OddEvenID(IEnumerable<T> TTypeIEnum)
{
return TTypeIEnum.Where(p => p.Id % 2 != 0).ToList();
}
public IEnumerable<T> Where_EqualsId(IEnumerable<T> TTypeIEnum, int id)
{
return TTypeIEnum.Where(p => p.Id == id).ToList();
}
}
public static void localmethod1(object a, object b)
{
Console.WriteLine("Local method." a "-" b);
}
public static void DisplayNumbers(int min, int max)
{
Console.WriteLine("DisplayNumbers :" (min max));
}
}
public class UserObject
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAdress { get; set; }
public DateTime CreateDateTime { get; set; } = DateTime.Now;
}
}
Result