Mohan
Mohan

Reputation: 969

DataType returned by linq query asp.net

I want to make a public function having multiple table joins in LINQ query. What will be data type of the function returned by function.

thank you

Upvotes: 2

Views: 1097

Answers (3)

Waqas Raja
Waqas Raja

Reputation: 10870

There are many possible options however, a simplest solution is to use object data type.

/// your public method
public object GetProducts()
{
    // your complex query
    // then return the anonymous type
    retutn query.ToList();
}

Then bind directly this object to the gridvew.

gridView1.DataSource = myObject.GetProducts();
gridView1.DataBind();

Upvotes: 4

Alireza
Alireza

Reputation: 5533

In addition to Frazell's answer: If you want your query to have a known return type, this is totally possible:

You'll have to create a simple class, say Result, with the properties you want to include in the query result:

public class Result
{
    public string Name
    {
        get;
        set;
    }
}

and then, instead of creating an anonymous type like: select new {Name = ...}, you can created a named type like: select new Result {Name = ...}. Then the outcome of the query becomes IQueriable<Result>. If you call .ToList() it becomes List<Result> and by calling .Single() you get a simple Result

Upvotes: 2

Frazell Thomas
Frazell Thomas

Reputation: 6111

It will be the type of the object resulting from your call. So if your function will return an object of type string then it will be a string.

LINQ makes use of anonymous types so that it can be of the same type as the objects in the expression. New types can also be created dynamically if needed.

You may want to review MSDN on LINQ.

Upvotes: 0

Related Questions