1

I am new to linq to sql

I wrote this function:

public ICollection<ICustomer> GetAll()
{
    DataClasses1DataContext context = new DataClasses1DataContext();
    var customers = from customer in context.Customers select customer;
    return customers.ToList().Cast<ICustomer>().ToList();
}

But it always return list of null values.

The database contain 3 records "filled with data" but this function return 3 nulls.

how to fix that?

2
  • Please show the definition of class Customer (first line would do). Commented Sep 30, 2009 at 2:56
  • you can see definition and more details in this question stackoverflow.com/questions/1495574/… Commented Sep 30, 2009 at 8:27

2 Answers 2

1

It may not be able to cast the results properly, have you made your partial Customer object implement ICustomer? If not, that is the reason.

Also you don't have to bring it to a list twice, or even once for that matter since you aren't returning a list, it might be more appropriate to change your signature to List or IEnumerable depending on your usage.

You can test whether or not the cast is succeeding by doing a simple test.

DataClasses1DataContext context = new DataClasses1DataContext();
var customers = from customer in context.Customers select customer;
int numberOfCustomers  = customers.Count();
var myCustomers = customers.Cast<ICustomer>(); //you could also do .OfType<ICustomer>();
int numberOfICustomers = myCustomers.Count();

If numberOfCustomers is 3 and numberOfICustomers is 0 then you know that was the issue.

Sign up to request clarification or add additional context in comments.

2 Comments

Re: Cast<T>() I made that mistake too. Cast will throw an exception if any of the elements can't be converted to the target type, not return null.
@Rex I was really hoping that it would throw an exception although I haven't done any accidental casting yet with linq (if you can believe it). I'm not sure what is going on but hopefully still testing the counts at the different filtering stages will provide the op with some help.
1

Your problem is almost certainly at the .Cast() method (confirm this by stepping through your code & ensuring that customers is populated correctly).

Does the Customer object implement the ICustomer interface? It sounds like an obvious thing to check but that would be a likely problem.

5 Comments

Cast<T> throws an exception if any of the elements can't be cast to the target type, so OP would not be seeing three nulls.
the problem happen in the Cast, because before it everything works
Without seeing your Customer & ICustomer, I can't test and replicate here. Is it null inside the function, or is it null when you are assigning the returned value to another object?
As Rex points out, the Cast is not throwing an exception so it must be 'successful' (in some sense).
Please see this question to see classes headers: stackoverflow.com/questions/1495574/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.