How to handle data types returned by LINQ

I'm new to LINQ, I used LINQ to SQL to communicate with two tables, it returns data, which is great. I'm trying to understand what a data type is returning and how can I work with this data type?

I am used to working with datatables. Do we upload LINQ data? (And all other ADO.Net objects, for example, rows, data sets, etc.)? If so, what will we replace it with, and how can I use it to do everything I did before using datatables? In addition, it makes sense to replace the data, do they have a drawback?

Here is the code:

protected IEnumerable<string> GetMarketCodes()
{
    LINQOmniDataContext db = new LINQOmniDataContext();

    var mcodes = from p in db.lkpMarketCodes 
                    orderby 0
                    select p;

    return (IEnumerable<string>) mcodes;
}

This code is currently returning data (I see it in debugging), but there are errors in the "return" line, because apparently my data type is not IEnumerables, and that was my best guess. So, another thing that I would like to understand is that the data type is my data that is being entered and how to return it to the calling function.

+3
source share
4 answers

It returns IQueryable<lkpMarketCode>, considering what lkpMarketCodeis the data type in db.lkpMarketCodes. If you need strings, you need select p.SomeProperty;, not just select p;.

You do not need to quit (as it IQueryable<T>implements IEnumerable<T>); he should also report this if you hover over mcodes.

+4

List < > , , . , :

protected List<string> GetMarketCodes()
{
    LINQOmniDataContext db = new LINQOmniDataContext();

    var mcodes = from p in db.lkpMarketCodes 
                    orderby 0
                    select p.SomeProperty;

    return mcodes.ToList();
}

, LINQ-to-SQL, , , .

+4

IQueryable. ? , , lkpMarketCodes - . .

IEnumerable , - ( , ):

var mcodes = from p in db.lkpMarketCodes 
orderby 0
select new { p.StringColumnName };
+3

LINQ IQueryable <type> s. IEnumerable. , , , IQueryable <string> IQueryable <lkpMarketCodes> . lkpMarketCodes - , .

LINQ - - , .

ADO , ADO, , , .

, lkpMarketCodes - , , mcode description.

IEnumerable <string> mcode's, - :

protected IEnumerable<string> GetMarketCodes()
{
    LINQOmniDataContext db = new LINQOmniDataContext();
    var mcodes = from p in db.lkpMarketCodes 
        orderby 0 
        select p.mcode;
    return mcodes;
}

IEnumerable <string> . , , - , , .

That is, if you hover over the returned mcodes, it will tell you the type, but it will not tell you the type if you hover over var mcodes.

0
source

Source: https://habr.com/ru/post/1703308/


All Articles