A couple of things your query returns a Anonymous type and NOT a Customer. We know this because in your select clause you have this, select new {, which does not name the type to create and so an Anonymous type is created. Note in the code snippet I have changed that to this, new DtCustomer {, which states to create the new objects of type DtCustomer, the class I also added to the code snippet. An Anonymous type really has no use outside of the method that created it therfore the reason for creating a concrete type DtCustomer to return to the caller. I also changed the Take(1) to FirstOrDefault(). The Take() method returns, "If count is less than or equal to zero, source is not enumerated and an empty IEnumerable(Of T) is returned.", where the FirstOrDefault returns a null if no result was found, better for error checking.
private DtCustomer GetCustomerInfo(string id){ var customer = (from customer in dtCustomers.AsEnumerable() where customer.Field<string>("id") == id select new DtCustomer { CustomerId = customer.Field<int>("id"), CustomerEnName = customer.Field<string>("EnName"), CustomerArName = customer.Field<string>("ArName"), CustomerClass = customer.Field<string>("EnClass"), CustomerCellPhone = customer.Field<string>("CellPhone"), Telephone = customer.Field<string>("Telephone"), CustomerEmail = customer.Field<string>("Email"), CustomerIsActive = customer.Field<string>("Active"), CustomerNationalId = customer.Field<int>("NationalId"), CustomerNationalIdType = customer.Field<string>("NationalIdType") }).FirstOrDefault(); return customer;}public class DtCustomer{ public int CustomerId {get; set;} public string CustomerEnName {get; set;} public string CustomerArName {get; set;} public string CustomerClass {get; set;} public string CustomerCellPhone {get; set;} public string Telephone {get; set;} public string CustomerEmail {get; set;} public string CustomerIsActive {get; set;} public int CustomerNationalId {get; set;} public string CustomerNationalIdType {get; set;} }
ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.
One of a set of tools we're offering as a way to say thank you for being a part of the community.
A couple of things your query returns a Anonymous type and NOT a Customer. We know this because in your select clause you have this, select new {, which does not name the type to create and so an Anonymous type is created. Note in the code snippet I have changed that to this, new DtCustomer {, which states to create the new objects of type DtCustomer, the class I also added to the code snippet. An Anonymous type really has no use outside of the method that created it therfore the reason for creating a concrete type DtCustomer to return to the caller. I also changed the Take(1) to FirstOrDefault(). The Take() method returns, "If count is less than or equal to zero, source is not enumerated and an empty IEnumerable(Of T) is returned.", where the FirstOrDefault returns a null if no result was found, better for error checking.
Open in new window
Fernando