Home > Net >  C# how to return 1 row from List<T> object passed as parameter
C# how to return 1 row from List<T> object passed as parameter

Time:07-09

I need to return one row of List from my function Selectus.

So I pass to the function Selectus object that reflects database table fields and I need to return one row which match the parameter looking_for:

public static List<T> Selectus<T>(string looking_for)
{
  //all select data
  var db = OrmLiteBaza().Open();//opening database
  var select_all_list = db.Select<T>();//getting all data for <T> object works fine
            
  db.Dispose();
            
  //try to select one row - here I have trouble:
  var prop = typeof(T).GetProperties();//properties of passed <T> object
                       
  var list_selected_record = from records in select_all_list where prop[1].Name == looking_for select records;//tryin to select one record from <T> object as in looking_for variable
  return list_selected_record.ToList();//here one record should be returned
}

I do not know how to select one row from the list assuming that T parameter is vary. In SelectusT> method I want to pass as T different objects which reflect fields in database table rather than creatinig separate methods for each select. e.g. call Selectus, where object passed is public class ProductCodes { public int ID { get; set; } public string SapIndex { get; set; } public string SapName { get; set; } }. Then I want to call another Selectus<ProductTypes> for another table etc... So I want to write generic/overall method and use it universally for all types of my objects which reflects the fields of few database tables. The SapIndex property is always in the same place of all objects...

CodePudding user response:

Using prop[1] is incredibly fragile. Who says that the property you're currently interested in is always going to be in second place? What if someone adds another property tomorrow? What if not every T that you use have the same property in the second place on its list of properties? It is quite unclear what your actual goal is here and why you've taken the reflection route.

You would be better off using inheritance or interface implementation here. I'm going to use an interface in this answer, but either would work.

For the sake of clarity, let's assume there is a Code field in all your possible lists, and this is the property you're trying to match with.

  1. Define a reusable interface:
public interface ICodeEntity
{
    string Code { get; }
}
  1. Apply your interface to all of the classes that you intend to use for your Selectus method.
public class Person : ICodeEntity
{
    public string Code { get; set; }
    // And other properties
}

public class Document : ICodeEntity
{
    public string Code { get; set; }
    // And other properties
}
  1. Add a generic type constraint that limits the use of T only to types that implement your interface.
public static List<T> Selectus<T>(string code)
    where T : ICodeEntity
  1. You can now write your code in a way that it relies on the type in question having a Code property, and the compiler will help enforce it.
var db = OrmLiteBaza().Open();
var list = db.Select<T>().ToList();            
db.Dispose();

return list.Where(item => item.Code == code).ToList();
  1. Usage examples:
List<Person> peopleWithCodeABC = Selectus<Person>("ABC");

List<Person> documentsWithCodeXYZ = Selectus<Document>("XYZ");

// This will fail if Animal does not implement ICodeEntity
var compilerError = Selectus<Animal>("ABC");

CodePudding user response:

you can change code to return just one element

public static T Selectus<T>(string looking_for)
{
  //all select data
  var db = OrmLiteBaza().Open();//opening database
  var select_all_list = db.Select<T>();//getting all data for <T> object works fine
            
  db.Dispose();
            
  //try to select one row - here I have trouble:
  var prop = typeof(T).GetProperties();//properties of passed <T> object
                       
  var list_selected_record = from records in select_all_list where prop[1].Name == looking_for select records;//tryin to select one record from <T> object as in looking_for variable
  return list_selected_record.FirstOrDefault();//here one record should be returned
}
  •  Tags:  
  • c#
  • Related