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...
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With