简体   繁体   中英

Result type of Linq expression

I'm making an application with sql database,

I get my data like this:

var vAll= from a in vPRCEntities.tblProducts
          select a;

which variable is 'vAll' then?, becouse i wanna use that in a methode:

private void vGet( ?? vAll)
{
}

I thought its too trivial for an answer. Furthermore the real credit goes to Charleh who pointed out the missing part. Anyway here it is

Try:

private void vGet(IEnumerable<Product> vAll)

where Product has to be your type in tblProducts :)

You could use this as suggested:

private void vGet(IEnumerable<Product> vAll){ }

Keep in mind that Linq uses delayed execution wich means the query won't actually execute until you read from the collection. That is okay but if you read the collection in a different part of the application you might run into issues. For example, the connection might get closed before you have a chance to get the data.

To avoid this you could try something like this:

var vAll = (from a in vPRCEntities.tblProducts select a).ToList<Product>();

This forces the collection to be read right away and puts it into a List<> object. You can then write your method like this:

private void vGet(List<Product> vAll){ } //IEnumerable will also work

Delayed execution is no longer in play since the collection has been read and put into a list.

Note: if the amount of data is very large then all of that data will be in memory in the list and this might not be a good solution. Like most things - it depends.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM