Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return LINQ object from method?

I have method which has LINQ query and query return columns from multiple tables.

How can I return that LINQ results object and catch it in caller method iterate results and assign to model class?

public ??? GetLocation(string CustomerNum)
    {
        if (!string.IsNullOrEmpty(CustomerNum))
        {
            var results = from ca in _context.CUS_ADDRESS
                           join cad in _context.CUS_ADDRESS_DETAIL on ca.CUS_ADDRESS_ID equals cad.CUS_ADDRESS_ID
                          where (cad.PRIORITY_SEQ == 0) && (ca.MASTER_CUSTOMER_ID == CustomerNum)
                           select new
                           {
                               CustomerNumber = ca.MASTER_CUSTOMER_ID,
                               ca.ADDRESS_1,
                               ca.ADDRESS_2,
                               ca.ADDRESS_3,
                               ca.ADDRESS_4,
                               ca.CITY,
                               ca.STATE,
                               ca.COUNTRY_DESCR,
                               cad.ADDRESS_TYPE_CODE,
                               cad.ADDRESS_STATUS_CODE
                           };
            return results;
        }
        else
        {
            return null;
        }
    }

Caller method

var results = Data.GetLocation(CustomerNum)
if (results.Any())
{
   var location = results.FirstOrDefault();
   .....
   .....
 }

What will be the GetLocation return type?

like image 994
James123 Avatar asked Oct 08 '13 18:10

James123


1 Answers

Depending on how you are actually using the results, you could return an IQueryable instead of IQueryable<T>.

I've used this in some situations (using IEnumerable), like WebForms, that have dynamic binding (either through Eval or by using a BoundField for instance.

like image 108
julealgon Avatar answered Oct 29 '22 15:10

julealgon