Convert string to double in linq in C# [duplicate] - c#

This question already has answers here:
Get result function in LINQ without translate to store expression
(2 answers)
Problem with converting int to string in Linq to entities
(15 answers)
Closed 5 years ago.
Convert string to double using LINQ query and get data
data1 => string type
data2 => double type
Query :
var query = get to db
query= query.Where(W => double.Parse(W.data1) >= W.data2).ToList();
Error :
"LINQ to Entities does not recognize the method 'Double
ToDouble(System.String)' method, and this method cannot be translated
into a store expression."

Related

OrderBy and OrderByDescending query on a decimal stored as a string [duplicate]

This question already has answers here:
Linq order by decimal field sorts like a string?
(3 answers)
How to use decimal type in MongoDB
(4 answers)
Need to store high precision decimal values in MongoDB
(2 answers)
Closed 3 years ago.
I'm trying to performing OrderBy decimal property on Mongo DB collection using C# mongo driver. Since the decimal is storing has string, I am not able to achieve this. How can I perform the OrderBy on decimal property?
//In the model
public decimal PayAmount { get; set; }
//While creating a query
var predicate = PredicateBuilder.New<Documents.Job>();
predicate = predicate.And(x => x.Status == jobStatus);
var jobdata = context.Jobs.AsQueryable().Where(predicate);
var query = from a in jobdata select new { //selects Field here };
query = query.OrderByDescending(x => x.PayAmount); // this does not sort and PayAmount is decimal type
var jobs = await query.ToListAsync();
Try this
query.OrderByDescending(x => Convert.ToDecimal(x.PayAmount));
But really I found you need to write a custom serializer for decimal there are some examples around if you search for serializing decimal in mongo.
This is not tested and you might need to write a custom serializer for decimal as mentioned.

Error in expression [duplicate]

This question already has answers here:
Linq Convert.ToInt32 in Query
(2 answers)
Closed 4 years ago.
Can anyone help me with the following error?
LINQ to Entities does not recognize the method 'Int32
Int32(System.String)' method, and this method cannot be translated
into a store expression.
Below is my code, I am trying in several ways to fix this error, but I have not been successful:
public IEnumerable<Dia1> GetPendenciasByUser(int centroId)
{
var query = Db.Dia1S
.Join(Db.Cadastros, dia1 => dia1.PatientId, cad => cad.PatientId, (dia1, cad) => new { dia1, cad })
.Join(Db.Randomizacao, dia1 => dia1.dia1.PatientId, rand => rand.PatientId, (dia1, rand) => new { dia1, rand })
.Where(s => s.dia1.dia1.dtd1 == null ? (Convert.ToInt32(DateTime.Now - s.rand.RandomizacaoData)) > 1 : (Convert.ToInt32(Convert.ToDateTime(s.dia1.dia1.dtd1) - s.rand.RandomizacaoData)) > 1 )
.Select(s => s.dia1.dia1)
.ToList();
return query;
}
The error message is clear, LINQ doesn't know how to convert the Convert.ToInt32() function to SQL. You can either use direct casting like this:
(int)(DateTime.Now - s.rand.RandomizacaoData)
Or you'll have to execute the query and get the data, then convert it in memory using Convert.ToInt32() as you wish.

Mimic SQL IN Clause with LINQ [duplicate]

This question already has answers here:
Where IN clause in LINQ [duplicate]
(8 answers)
Linq to Entities - SQL "IN" clause
(9 answers)
Closed 8 years ago.
I am trying to mimic an IN() clause commonly used in SQL and use it in my LINQ statement.
I see that there is an overload for Contains() that takes an IEnumerable collection. I have tried passing in ILIST and Dictionary but neither is correct.
How do I accomplish this?
Thanks, much appreciated.
SQL
select
oec.OnlineEducationCourseId,
oec.CourseTitle,COUNT(oec.CourseTitle) as CourseCount
from OnlineEducationRegistration as oer
join OnlineEducationCourse oec on oec.OnlineEducationCourseId = oer.OnlineEducationCourseId
where oer.ClubId IN('K20','B67','P89')
and DateCompleted between '2013-01-01' and '2014-01-01'
group by oec.CourseTitle,oec.OnlineEducationCourseId
Same SQL query in LINQ
var r = (from oer in db.OnlineEducationRegistrations
join oec in db.OnlineEducationCourses on oer.OnlineEducationCourseId equals
oec.OnlineEducationCourseId
where (oer.ClubId.Contains(some IEnumerable collection here) && oer.DateCompleted >= start.Date && oer.DateCompleted <= end.Date)
group new { oec, oer } by new { oec.CourseTitle, oec.OnlineEducationCourseId }).ToList();

The specified type member 'Date' is not supported in LINQ to Entities [duplicate]

This question already has answers here:
The specified type member 'Date' is not supported in LINQ to Entities Exception
(9 answers)
Closed 9 years ago.
I have the following method inside my asp.net mvc web application :-
public SystemInformation GetSystemInfo(int pagesize)
{
var d = DateTime.Today;
SystemInformation s = new SystemInformation()
{
DeleteNo = tms.TechnologyAudits.Where(a => (EntityFunctions.TruncateTime(a.DateTimeEnd.Date) == EntityFunctions.TruncateTime(d) && a.AuditAction.Name.ToLower() == "delete")).Count(),
};
return s;
}
But the above is raising the following exception:-
The specified type member 'Date' is not supported in LINQ to
Entities. Only initializers, entity members, and entity navigation
properties are supported.
It is a.DateTimeEnd.Date that cannot be translated. The equivalent is EntityFunctions.TruncateTime(a.DateTimeEnd) . You do not need/cannot use both.
DateTime.Date :
A new object with the same date as this instance, and the time value
set to 12:00:00 midnight (00:00:00)
EntityFunctions.TruncateTime :
The input date with the time portion cleared.
LINQ to Entities cannot translate most .NET Date methods (including the casting you used) into SQL since there is no equivalent SQL.
In your case LINQ cannot translate this
(EntityFunctions.TruncateTime(a.DateTimeEnd.Date)
The solution is to cast the object outside the LINQ statement and then pass in a value.

LINQ to Entities does not recognize the method and this method cannot be translated into a store expression [duplicate]

This question already has answers here:
LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)'
(3 answers)
Closed 6 years ago.
var ps = dbContext.SplLedgers.Select(p => new SplLedgerModel
{
Name = p.Name,
VoucherType = Convert.ToString(((JIMS.VoucherTypes)p.VoucherType))
});
I am getting the following exception, whats wrong with the code.
JIMS.VoucherType is an enum
+ $exception {"LINQ to Entities does not recognize the method 'System.String ToString(System.Object)' method, and this method cannot be translated into a store expression."} System.Exception {System.NotSupportedException}
Your code is basically trying to find the Convert.ToString() method in the DB and understandably failing.
You can get around it, for example by making sure the query executes before the select, e.g.
var ps = dbContext.SplLedgers.Select(p => new
{
Name = p.Name,
VoucherType = p.VoucherType
}).ToList().Select(p => new SplLedgerModel(
{
Name = p.Name,
VoucherType = Convert.ToString(((JIMS.VoucherTypes)p.VoucherType))
});

Categories