0
\$\begingroup\$

I have three methods to get the user details based on attributes:

  • Find user details by id,
  • Find user details by username,
  • Find user details by email

Here is the program class:

using System;
using System.Data;
using System.Linq;
using System.Data.DataSetExtensions;
public class Program
{
 public static void Main()
 {
 Console.WriteLine("Hello World");
 var user = GetUserById(1);
 Console.WriteLine(user.Name);
 var user1 = GetUserByUsername("user3");
 Console.WriteLine(user1.Name);
 var user2 = GetUserByEmail("[email protected]");
 Console.WriteLine(user2.Name);
 }
 public static User GetUserById(int id)
 {
 var users = GetUsers().AsEnumerable().Where(row => row.Field<int>("Id") == id);
 var userInfo = users.Select(s => new User{
 Id = s.Field<int>("Id"),
 Username = s.Field<string>("Username"),
 Name = s.Field<string>("Name"),
 Email = s.Field<string>("Email"),
 CreatedDate = s.Field<DateTime>("CreatedDate"),
 Phone = s.Field<string>("Phone"),
 City = s.Field<string>("City"),
 State = s.Field<string>("State"),
 Country = s.Field<string>("Country")
 }).FirstOrDefault();
 return userInfo;
 }
 public static User GetUserByUsername(string username)
 {
 var users = GetUsers().AsEnumerable().Where(row => row.Field<string>("Username") == username);
 var userInfo = users.Select(s => new User{
 Id = s.Field<int>("Id"),
 Username = s.Field<string>("Username"),
 Name = s.Field<string>("Name"),
 Email = s.Field<string>("Email"),
 CreatedDate = s.Field<DateTime>("CreatedDate"),
 Phone = s.Field<string>("Phone"),
 City = s.Field<string>("City"),
 State = s.Field<string>("State"),
 Country = s.Field<string>("Country")
 }).FirstOrDefault();
 return userInfo;
 }
 public static User GetUserByEmail(string email)
 {
 var users = GetUsers().AsEnumerable().Where(row => row.Field<string>("Email") == email);
 var userInfo = users.Select(s => new User{
 Id = s.Field<int>("Id"),
 Username = s.Field<string>("Username"),
 Name = s.Field<string>("Name"),
 Email = s.Field<string>("Email"),
 CreatedDate = s.Field<DateTime>("CreatedDate"),
 Phone = s.Field<string>("Phone"),
 City = s.Field<string>("City"),
 State = s.Field<string>("State"),
 Country = s.Field<string>("Country")
 }).FirstOrDefault();
 return userInfo;
 }
 static DataTable GetUsers()
 {
 // Consider these are the data from the SQL table.
 DataTable table = new DataTable();
 table.Columns.Add("Id", typeof(int));
 table.Columns.Add("Username", typeof(string));
 table.Columns.Add("Name", typeof(string));
 table.Columns.Add("Email", typeof(string));
 table.Columns.Add("CreatedDate", typeof(DateTime));
 table.Columns.Add("Phone", typeof(string));
 table.Columns.Add("City", typeof(string));
 table.Columns.Add("State", typeof(string));
 table.Columns.Add("Country", typeof(string));
 // Here we add five DataRows.
 table.Rows.Add(1, "user1", "David", "[email protected]", DateTime.Now, "9999999999", "City 1", "State 1", "India");
 table.Rows.Add(2, "user2", "Sam", "[email protected]", DateTime.Now, "8888888888", "City 2", "State 2", "USA");
 table.Rows.Add(3, "user3", "Christoff", "[email protected]", DateTime.Now, "7777777777", "City 3", "State 3", "UK");
 table.Rows.Add(4, "user4", "Janet", "[email protected]", DateTime.Now, "6666666666", "City 4", "State 4", "Germany");
 table.Rows.Add(5, "user5", "Melanie", "[email protected]", DateTime.Now, "5555555555", "City 5", "State 5", "France");
 return table;
 }
}
public class User
{
 public int Id {get;set;}
 public string Username {get;set;}
 public string Name {get;set;}
 public string Email {get;set;}
 public DateTime CreatedDate {get;set;}
 public string Phone {get;set;}
 public string City {get;set;}
 public string State {get;set;}
 public string Country {get;set;}
}

Is there any possibility to reuse the below object in LINQ using predicate?

new User{
 Id = s.Field<int>("Id"),
 Username = s.Field<string>("Username"),
 Name = s.Field<string>("Name"),
 Email = s.Field<string>("Email"),
 CreatedDate = s.Field<DateTime>("CreatedDate"),
 Phone = s.Field<string>("Phone"),
 City = s.Field<string>("City"),
 State = s.Field<string>("State"),
 Country = s.Field<string>("Country")
 }

DotNet Fiddle: https://dotnetfiddle.net/psiBtv

asked Oct 25, 2018 at 13:48
\$\endgroup\$
3
  • 4
    \$\begingroup\$ Please post real code that does compile. This code is incomplete and does not compile. This question seems more suited for stackoverflow. Btw., a solution would be to add a constructor to User accepting the a parameter and doing the assignments internally, so you could write new User(a). \$\endgroup\$ Commented Oct 25, 2018 at 14:11
  • 2
    \$\begingroup\$ return Data.Select(SelectFunc).FirstOrDefault(); ... then write SelectFunc as a function that takes a single parameter of type 'a' and User as the return type. \$\endgroup\$ Commented Oct 25, 2018 at 22:31
  • \$\begingroup\$ I've edited the code sample @OlivierJacot-Descombes \$\endgroup\$ Commented Oct 26, 2018 at 4:42

1 Answer 1

1
\$\begingroup\$

I've used Func<> to reuse the object in Linq. Here is the working sample.

using System;
using System.Data;
using System.Linq;
using System.Data.DataSetExtensions;
public class Program
{
 private static readonly Func<DataRow, User> fnUserInfo = s => 
 new User
 {
 Id = s.Field<int>("Id"),
 Username = s.Field<string>("Username"),
 Name = s.Field<string>("Name"),
 Email = s.Field<string>("Email"),
 CreatedDate = s.Field<DateTime>("CreatedDate"),
 Phone = s.Field<string>("Phone"),
 City = s.Field<string>("City"),
 State = s.Field<string>("State"),
 Country = s.Field<string>("Country")
 };
 public static void Main()
 {
 Console.WriteLine("Hello World");
 var user = GetUserById(1);
 Console.WriteLine(user.Name);
 var user1 = GetUserByUsername("user3");
 Console.WriteLine(user1.Name);
 var user2 = GetUserByEmail("[email protected]");
 Console.WriteLine(user2.Name);
 }
 public static User GetUserById(int id)
 {
 var users = GetUsers().AsEnumerable().Where(row => row.Field<int>("Id") == id);
 var userInfo = users.Select(fnUserInfo).FirstOrDefault();
 return userInfo;
 }
 public static User GetUserByUsername(string username)
 {
 var users = GetUsers().AsEnumerable().Where(row => row.Field<string>("Username") == username);
 var userInfo = users.Select(fnUserInfo).FirstOrDefault();
 return userInfo;
 }
 public static User GetUserByEmail(string email)
 {
 var users = GetUsers().AsEnumerable().Where(row => row.Field<string>("Email") == email);
 var userInfo = users.Select(fnUserInfo).FirstOrDefault();
 return userInfo;
 }
 static DataTable GetUsers()
 {
 // Consider these are the data from the SQL table.
 DataTable table = new DataTable();
 table.Columns.Add("Id", typeof(int));
 table.Columns.Add("Username", typeof(string));
 table.Columns.Add("Name", typeof(string));
 table.Columns.Add("Email", typeof(string));
 table.Columns.Add("CreatedDate", typeof(DateTime));
 table.Columns.Add("Phone", typeof(string));
 table.Columns.Add("City", typeof(string));
 table.Columns.Add("State", typeof(string));
 table.Columns.Add("Country", typeof(string));
 // Here we add five DataRows.
 table.Rows.Add(1, "user1", "David", "[email protected]", DateTime.Now, "9999999999", "City 1", "State 1", "India");
 table.Rows.Add(2, "user2", "Sam", "[email protected]", DateTime.Now, "8888888888", "City 2", "State 2", "USA");
 table.Rows.Add(3, "user3", "Christoff", "[email protected]", DateTime.Now, "7777777777", "City 3", "State 3", "UK");
 table.Rows.Add(4, "user4", "Janet", "[email protected]", DateTime.Now, "6666666666", "City 4", "State 4", "Germany");
 table.Rows.Add(5, "user5", "Melanie", "[email protected]", DateTime.Now, "5555555555", "City 5", "State 5", "France");
 return table;
 }
}
public class User
{
 public int Id { get; set; }
 public string Username { get; set; }
 public string Name { get; set; }
 public string Email { get; set; }
 public DateTime CreatedDate { get; set; }
 public string Phone { get; set; }
 public string City { get; set; }
 public string State { get; set; }
 public string Country { get; set; }
}

DotNetFiddle: https://dotnetfiddle.net/FhDRR6

answered Oct 27, 2018 at 8:06
\$\endgroup\$
2
  • \$\begingroup\$ Is there any other way or better approach to achieve this? \$\endgroup\$ Commented Oct 27, 2018 at 8:09
  • 1
    \$\begingroup\$ Any method with a DataRow parameter and a return type of User can be used in your Select. In this case, a static method will do fine, there's no need to explicitly declare it as a Func. \$\endgroup\$ Commented Oct 27, 2018 at 11:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.