Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a method inside the select part of a LINQ query

I have the following linq query:

var file = (from p in database.tblFile
            join o in database.tblFileVersion on p.fldFileID equals o.fldFileID
            join t in database.tblFileAttachments on p.fldFileID equals t.fldFileID
            where p.fldFileID == team.Key
            where o.fldVersionNo == highestVersion
            select new UserDashboardFile
            {
                Filename = p.fldFilename,
                VersionNumber = o.fldVersionNo,
                FileID = team.Key,
                Type = GetFileType(t.fldTableName),
            }).Single();

GetFileType is a method that returns an enumeration type. There is no syntax error but when I run the project (it is an mvc 4 web project) I receive the following error:

LINQ to Entities does not recognize the method 'DCIS.Code.UserDashboardFileType GetFileType(System.String)' method, and this method cannot be translated into a store expression.

I imagine that this query cannot be translated into a t-sql query but I do not know how to change my query in order to avoid the above error. Thanks in advance...

like image 560
Giorgos Manoltzas Avatar asked Nov 11 '12 16:11

Giorgos Manoltzas


People also ask

What does SelectMany do in LINQ?

The SelectMany in LINQ is used to project each element of a sequence to an IEnumerable<T> and then flatten the resulting sequences into one sequence. That means the SelectMany operator combines the records from a sequence of results and then converts it into one result.

What is let clause in LINQ?

The Let keyword allows you to create a range variable and initialized with the result of the query expression and then you are allowed to use that variable with the upcoming clause in the same query.

Why from comes before Select in LINQ?

LINQ is based on C# which is a declarative language, means whenever you want to use any variable name, it must have been already been declared beforehand. In LINQ queries, when you write "From" first - it declares the variable whoch can later be used in "Select" statement.


1 Answers

You can select anonymous object first and then construct UserDashboardFile.

var file = (from p in database.tblFile
            join o in database.tblFileVersion on p.fldFileID equals o.fldFileID
            join t in database.tblFileAttachments on p.fldFileID equals t.fldFileID
            where p.fldFileID == team.Key
            where o.fldVersionNo == highestVersion
            select new 
            {
                Filename = p.fldFilename,
                VersionNumber = o.fldVersionNo,
                FileID = team.Key,
                FldTableName = t.fldTableName
            }).Single();
var udFile = new UserDashboardFile { ..., Type = GetFileType(file.FldTableName) };

Also you can call .AsEnumerable() before select to prevent GetFileType to be translated in sql.

like image 87
tukaef Avatar answered Oct 24 '22 07:10

tukaef