Get any Item from any table using Linq to Sql RRS feed

  • General discussion

  • I saw this code in one page

    public TEntityType GetItem<TEntity>(string propertyName, string propertyValue) where TEntityType : class 
    and it can be called in this way:

    Customer Customer = GetItem<Customer>("Name","ABC");

    I would like to implement this method but I am new to Linq to Sql.

    So my question is how to implement this method.
    Wednesday, January 6, 2010 4:02 PM

All replies

  • What does it have to do with LINQ or SQL? What should the function do?

    Thursday, January 7, 2010 5:10 PM
  • It should select a record from customer table by using a column name and  that column value.

    Customer Customer1 = GetItem<Customer>("LastName","ABC");
    Customer Customer2 = GetItem<Customer>("City","XYZ"); .
    Thursday, January 7, 2010 6:10 PM
  • You can use the below query to get the customer with last name as "ABC"


    var cust = from c in db.Customers
                   where c.LastName== "ABC"


                   select new {c.CustomerID, c.ContactName , c.Address};

    Friday, January 8, 2010 3:07 AM
  • but if you like to search for FirstName, City,... in the same method using generic.

    any way, I cancled the idea to use above method and follow different Linq 2 Sql pattren.

    thanks for all

    Friday, January 8, 2010 2:52 PM
  • I think  generics are not required in this case. If you got answer using generics then let me know as well :)
    But you can search using Last name, first name city etc.. as like below:
    cust = from c in
                   where c.LastName==
    "ABC" AND c.FirstName == "PQR" AND c.City == "XYZ"

                    select new {c.CustomerID, c.ContactName , c.Address};

    Monday, January 11, 2010 2:54 AM