views:

457

answers:

2

EDIT 1

I apologize but after reading the 2 suggested articles I still don't understand what I should use. I understand that using IQueryable is not preferred for various reasons but does that eliminate IEnumerable as well? Is a DataTable really my best option?

In short, I guess, what is the preferred Return type?


I have the following simple LINQ query that I want to abstract out into a DAL. What is the type of var and therefore what type should my method be?

            ConnectDBDataContext context = new ConnectDBDataContext();

        var lName = textEdit1.Text;

        var searchByPersonLName = from c in context.tblPersons
                                  where c.LastName == lName
                                  orderby c.LastName
                                  select new { c.FirstName,c.LastName,c.PersonID};

        dataGridView1.DataSource = searchByPersonLName;

When I hover over it in VS it says IQueryable<T> but when I put in a breakpoint and run it it seems to call itself IEnumerable. Which is correct and how should I declare my method?

Like this -->

        public static DataTable SearchPerson(string SearhParam)
    {
        ConnectDBDataContext context = new ConnectDBDataContext();
        var persons = (from person in context.tblPersons
                       orderby person.LastName
                       select new { person.PersonID, person.LastName, person.FirstName, person.SSN });
        var filteredPersonsList = persons.Where(p => p.LastName == SearhParam).ToList();
        if (filteredPersonsList.Count == 0)
            filteredPersonsList = persons.Where(p => p.LastName.StartsWith(SearhParam)).ToList();

        var dataTable = filteredPersonsList.CopyLinqToDataTable();

        return dataTable;
    }

If I use IQueryable<T> what is <T> or how do I know that and what would I return?

Thanks!

For Reference the CopyToDataTable() is below.

public static DataTable CopyLinqToDataTable<T>(this IEnumerable<T> source)
    {
        return new ObjectShredder<T>().Shred(source, null, null);
    }

    public static DataTable CopyLinqToDataTable<T>(this IEnumerable<T> source,
                                                DataTable table, LoadOption? options)
    {
        return new ObjectShredder<T>().Shred(source, table, options);
    }
+2  A: 

First off, IQueryable implements IEnumerable, so that is why you are potentially seeing both. See here for more details

Generally, I would recommend your DAL return your actually objects whenever possible.

I would read this blog for guidelines on how to, and how not to do what you are suggesting. Short answer, don't return IQueryable.

EDIT: Example:

        internal static File[] GetAllFilesByUserID(int userID)
    {
        var db = GetDataContext();
        return (from files in db.Files where files.OwnerUserID == userID select files).ToArray();
    }
Serapth
Thank you, what do you mean "Actual Object" Can you give me a quick example?
Refracted Paladin
Se Davids post, it explains what I meant. Generally I use the classes created by L2S, or an array there of. I will edit in an example of how my DAL returns File objects from my FileLibrary as an example.
Serapth
+2  A: 

What he means is to map your data to the object you are wanting the DAL to return.

In answer to your first question "var" is really just short for variable, and the type is what ever type is defined in the assignment.

var myvariable = string.empty;

In this example the type is that of a string.

var myreader = new StringReader();

While in this example the type is that of a StringReader.

As for your second question of "what is ". T is a generic type.

For an example of where your dal would be returning an actual object:

 public Product GetProduct(int ProductID)
    {
        var product = from p in db.MyTable
                      where p.productID == ProductID
                      select new product { name = p.name, pricepoint = p.pricepoint, qty = p.quantity };

        return product;
    }
David Yancey