Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to SQL - Don't fetch a particular column

Is there a way to not fetch a specific column using linqtosql without having to use an anonymous type and specify each returned filed individually?

We use SQLMetal to generate the dbml file which contains all the types into which to put the queried data results. However, when select columns are included in the linq query, the results go into an anonymous type instead of the type declared in the dbml file. I would like to select all but one of the columns from a particular table, but still have the results returned in the related dbml type.

Any ideas appreciated.

like image 363
James Avatar asked May 12 '09 13:05

James


People also ask

How do I select a single column in LINQ?

var Q1 = (ds. Tables[1]. AsEnumerable() .

Is LINQ to SQL deprecated?

No it is not.

How does a LINQ query transform to a SQL query?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.

Is LINQ better than SQL?

More importantly: when it comes to querying databases, LINQ is in most cases a significantly more productive querying language than SQL. Compared to SQL, LINQ is simpler, tidier, and higher-level. It's rather like comparing C# to C++.


2 Answers

LINQ to SQL supports lazy loading individual properties. In the DBML designer, you can set Delay Loaded to true in a column's properties. Columns that are delay loaded will not be included in the initial SELECT. If you try to access the object's property and it has not been loaded yet, then another SELECT statement will be executed to bring this value from the DB.

If you are editing the DBML file by hand, set <Column IsDelayLoaded="true">. If you are writing your LINQ to SQL classes by hand, it is as simple as declaring the property's backing field as a Link<T> instead of T. For example:

[Table]
public class Person
{
    private Link<string> _name;

    [Column(Storage = "_name")]
    public string Name
    {
        get { return _name.Value; }
        set { _name.Value = value; }
    }
}

See also the "Delay/Lazy Loading" section in this post by Scott Guthrie.


Update: The above answer applies if you want the column to still be available when you need it. It will not be included in SELECTs unless you specifically ask for it (see LoadOptions) or try to access it.

If you just don't want to use or access the column at all, and you don't want it available as a class property, then go with Serapth's answer of removing the column from the DBML file. Be sure you understand the implications, such as the loss of concurrency checking on that column.

like image 167
Lucas Avatar answered Oct 12 '22 18:10

Lucas


If you are extremely lazy, why not add the table a second time via DBML designer, rename it MyTableWithOutColumnX then delete the record you don't want returned? Just hilite the column name and hit delete.

Its sloppy, but then, to a certain degree, so is having a table where a record shouldn't appear sometimes. More then anything, it is easy, its all abstracted in the DBML so it shouldnt impact your code, other than switching which table to access. Well named, it might even make sense to someone maintaining your code in the distant future.

like image 29
Serapth Avatar answered Oct 12 '22 17:10

Serapth