Created
July 3, 2023 16:09
-
-
Save SKumarSpace/559ef3d607ecd6bd6dd6bc7320f375bb to your computer and use it in GitHub Desktop.
Pass C# Property via LINQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace PassPropertyTests | |
{ | |
public class BaseObject | |
{ | |
public string Name { get; set; } | |
public string Email { get; set; } | |
} | |
public class MyService<T> where T : BaseObject, new() | |
{ | |
private readonly List<T> Data = new List<T> | |
{ | |
new T { Name = "JAMES", Email = "abc" }, | |
new T { Name = "SHREY", Email = "def" } | |
}; | |
public void Login(string email) | |
{ | |
Login(email, x => x.Name); | |
} | |
public void Login(string email, Func<T, string> selector) | |
{ | |
var value = Data.SingleOrDefault(x => selector(x) == email); | |
Console.WriteLine($"Login2: {value?.Email}"); | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
var service = new MyService<BaseObject>(); | |
service.Login("JAMES"); | |
service.Login("def", x => x.Email); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment