Skip to content

Instantly share code, notes, and snippets.

@SKumarSpace
Created July 3, 2023 16:09
Show Gist options
  • Save SKumarSpace/559ef3d607ecd6bd6dd6bc7320f375bb to your computer and use it in GitHub Desktop.
Save SKumarSpace/559ef3d607ecd6bd6dd6bc7320f375bb to your computer and use it in GitHub Desktop.
Pass C# Property via LINQ
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