I am trying to get range data between two strings.
i have list of string which have data like this
Example:
K123456,
H123456,
J123456,
T122123,
So the first charter of string object contains an alphabet.
var StartLabwareid = int.Parse(obj.LWbStartId.Replace(labIni,""));
var EndtLabwareid = int.Parse(obj.LWbEndId.Replace(labIni, ""));
var LabWId = (from ai in _entities.Ai
join result in _entities.Re on ai.Id equals re.Id
where (Convert.ToInt32(ai.Id.Replace(Ini,"")) <= Startid ||
Convert.ToInt32(ai.Id.Replace(Ini, "")) >= Endtid)
select new MViewModel
{
ID = re.Id,
}).ToList();
Or I can use something else to compare to string and get data between that as we do in date range St rating and Ending Date
You cannot use C# functions such as Convert in Linq to Entities. Instead, you could do your comparison after the result is materialized, or use SqlFunctions. With SqlFunctions, you should get your result with combining Stuff and CharIndex functions. Modify the line below to suit your needs, since you are not providing all of the code.
SqlFunctions.Stuff(ai.Id, SqlFunctions.CharIndex(ai.Id, "replaceable"), 3, "")
Related
I used two strings, one for today's date and one for the database, which is the date that users register. I want to convert these two strings to a solar date and compare them.
This code works well and I converted the first variable correctly and I can compare it
PersianDateTime now = PersianDateTime.Now;
string s = now.ToString("yyyy/MM/dd");
PersianDateTime persianDate = PersianDateTime.Parse(s);
But this code gives an error because it becomes a condition
var ActivitysNotToDo = _context.Activitys.Where(a => a.MasoolAghdamUserID == user.Id && PersianDateTime.Parse(a.ActvityAghdamDate) < persianDate).ToList();
An error occurred in this code. Please also see the photo
PersianDateTime.Parse(a.ActvityAghdamDate)
If the first code works for you, then:
Maybe you can modify this line:
var ActivitysNotToDo = _context.Activitys.Where(a => a.MasoolAghdamUserID == user.Id && PersianDateTime.Parse(a.ActvityAghdamDate) < persianDate).ToList();
For this:
var ActivitysNotToDo = _context.Activitys.Where(a => a.MasoolAghdamUserID == user.Id && PersianDateTime.Parse(Convert.ToDateTime(a.ActvityAghdamDate).ToString("yyyy/MM/dd")) < persianDate).ToList();
DateTime.Parse(); function need string parameter so you have to pass string parameter in your expression.
DateTime now = DateTime.Now;
string s = now.ToString("yyyy/MM/dd");
DateTime.Parse(s);
DateTime.Parse(now.ToString()); // Conversion required
Try following code.
PersianDateTime.Parse(a.ActvityAghdamDate.Value.ToString("yyyy/MM/dd"))
or
PersianDateTime.Parse(Covert.ToDateTime(a.ActvityAghdamDate).ToString("yyyy/MM/dd"))
Implicit client evaluation has been disabled from EF Core 3. You can change your code like below:
var data= _context.Activitys.Where(a => a.MasoolAghdamUserID == user.Id)
.AsEnumerable();
var ActivitysNotToDo = data.Where(a => PersianDateTime.Parse(a.ActvityAghdamDate)<persianDate)
.ToList();
The C# Compare() method is used to compare first string with second string lexicographically. If both strings are equal, it returns 0.
below query return a single value eg 50. I want it be assign to int so I can do some calculation with that value. how can I do this
var LTLimit = from a in dbavailability.tACLicenseTypes
where a.License_Type == "Long Term"
select a.Limit;
string AST = "";
I'm not completely clear on what you're asking, but presumably the type of data returned by your Linq query is not int, and you want to convert it to int? If so, simply convert it to an int:
var LTLimit = (from a in dbavailability.tACLicenseTypes
where a.License_Type == "Long Term"
select a.Limit).ToList();
int LTLimitInt = 0
if (!int.TryParse(LTLimit.First(), out LTLimitInt))
{
Console.WritLine("LTLimit is not a number!")
}
Since you've updated your question, here's a solution to convert the returned number to an int, multiply it by 2, then convert the result to a string:
var LTLimit = (from a in dbavailability.tACLicenseTypes
where a.License_Type == "Long Term"
select a.Limit).ToList();
int LTLimitInt = 0;
string multipliedResultStr = string.Empty;
if (!int.TryParse(LTLimit.First(), out LTLimitInt))
{
Console.WritLine("LTLimit is not a number!")
}
else
{
multipliedResult = (LTLimitInt * 2).ToString();
Console.WriteLine(string.Format("Result is {0}, multipliedResult ));
}
Edit;
Corrected code to take first item from list.
The following code is one way to retrieve the first integer returned in your query:
var LTLimit = (from a in dbavailability.tACLicenseTypes
where a.License_Type == "Long Term"
select a.Limit).ToList();
int limit = LTLimit[0];
What is the return type of the query?
If the returned type is of another primative data type like a string, then you can try to convert it to an integer using :
var i = Convert.ToInt32(LTLimit);
Or using :
int.TryParse(LTLimit, out var i);
However, it would be better if the data was stored in the correct type in the first place.
You can use Sum() after where() for value of object property
like this bonus.Where(b => b.Id == x.key && b.RequiredScore.HasValue).Sum(b => b.RequiredScore.Value);
Im trying to do a simple string comparison in linq between two strings.
The strings are sizes and some of the sizes have a decimal within the string like "2.75". I get an error when comparing strings in the table against strings with decimals, saying "Unable to cast object of type 'System.Single' to type 'System.String'" Type checking within the debugger reveals that it is a string before the comparison. Why is this happening, and how can I make sure that strings with decimals inside get treated as strings?
var results = from item in _table.AsEnumerable()
where item.Field<string>("Width") == _size.Width &&
item.Field<string>("Length") == _size.Length
select item;
You need to convert the read values into singles:
var results = from item in _table.AsEnumerable()
where Convert.ToSingle(item.Field<string>("Width")) == _size.Width &&
Convert.ToSingle(item.Field<string>("Length")) == _size.Length
select item;
or
var results = from item in _table.AsEnumerable()
let width = Convert.ToSingle(item.Field<string>("Width"))
let length = Convert.ToSingle(item.Field<string>("Length"))
where (width == _size.Width) && (length == _size.Length)
select item;
to make it more readable
How can I combine variables to a string and use the string in a where condition within a LINQ query? I have tried this:
query = query.Where(p => (p.Cod + p.Year + p.Count).StartsWith("BS201412"));
My variables are as follows:
Cod is of type string
Year is of type short
Count is of type int
I have also tried this:
query = query.Where(p => (p.Cod + SqlFunctions.StringConvert((double) p.Year)+ SqlFunctions.StringConvert((double) p.Count)).StartsWith("BS201412"));
but because of Year the query is not working in both variants.
You have many options and two of them are:
use String.Format - it converts automatically every parameter to string
use the ToString method on the complex variable
Example:
string cod = "BS";
short year = 14;
int count = 100;
Console.WriteLine(String.Format("{0}{1}{2}", cod, year, count));
Console.WriteLine(cod + year.ToString() + count.ToString());
The output is in both cases the same:
BS14100
BS14100
Your query line could look like this:
query = query
.Where(p => String.Format("{0}{1}{2}", p.Cod, p.Year, p.Count)
.StartsWith("BS201412"));
Because you are using LINQ2SQL some of the functions can not be used (because they can not be translated to SQL directly). You can try a different approach - it looks like you have the String you want to search for BS201420, so you can partition the string, convert every part to the corresponding type and write a normal query, something like this:
var searchFor = "BS201420";
var cod = searchFor.Substring(0, 2);
// example - use short.TryParse in your code
var year = short.Parse(searchFor.Substring(2, 4));
// example - use int.TryParse in your code
var count = int.Parse(searchFor.Substring(4,2));
query = query.Where(p => p.Cod == cod && p.Year == year && p.Count == count);
This should find the same result set as with the string and starts with.
I am using dynamic Linq to return data for user-input search criteria. My query is working fine except for the user selected dates. My current code is:
StringBuilder whereClause = new StringBuilder();
if (startDate.HasValue || endDate.HasValue)
{
DateTime searchStartDate = startDate.HasValue ? startDate.Value : DateTime.MinValue;
DateTime searchEndDate = endDate.HasValue ? endDate.Value : DateTime.MaxValue;
whereClause.AppendFormat("Date >= {0} && Date <= {1}",
searchStartDate.Date.ToUniversalTime(),
searchEndDate.Date.ToUniversalTime());
}
if (whereClause.Length > 0)
{
return (from p in this.repository.GetQueryable<Party>() select p)
.Where(whereClause.ToString())
.ToList();
}
The query falls over because the comparison is being done between a DateTime field and a Int32 field, meaning the query has interpreted my date literals as integers.
How should I be formatting the dates?
Use
.Where("Date >= #0 && Date <= #1",
searchStartDate.Date.ToUniversalTime(),
searchEndDate.Date.ToUniversalTime())
instead.
In reply to Val's comment:
OK, then you can do:
whereClause.AppendFormat("Date.ToString() >= \"{0}\" && Date.ToString() <= \"{1}\"",
searchStartDate.Date.ToUniversalTime(),
searchEndDate.Date.ToUniversalTime());
You have to convert the Date in the query to a string and then compare it a quoted string literal. Without the quotes the parser is inerpreting the numbers inserted into the where clause as integers - what should explain the error you originally got.
Why are you parsing strings in a LINQ expression? The entire point of LINQ is to avoid that.
var q = from p in this.repository.GetQueryable<Party>() select p;
if (startDate.HasValue || endDate.HasValue)
{
var searchStartDate = startDate.HasValue ? startDate.Value : DateTime.MinValue;
var searchEndDate = endDate.HasValue ? endDate.Value : DateTime.MaxValue;
return
q.Where (p=> p.Date >= searchStartDate.ToUniversalTime()
&& p.Date <= searchEndDate.ToUniversalTime()).ToList();
}
return q.ToList();
UPDATE:
In response to comments: I'm building that one at run-time. The question isn't run-time vs compile-time; it's "in strings" vs "in code". StringBuilder lets you append text; LINQ lets to chain lamdbas. It all works out the same --- except your code is type-safe and syntax checked using lambdas.
To demostrate this concept further, the following code compiles & runs fine, and allows to you to change the Where clause based on the values of oddsOnly and lowerLimit.
int[] nums = {1,2,3,4,5,6,7,8,9,10};
bool oddsOnly = true;
bool lowerLimit = 5;
var q = from i in nums select i;
if (oddsOnly)
q = q.Where( n=> n%2 == 1);
if (lowerLimit != 0)
q = q.Where( n=> n >= lowerLimit);
foreach(var i in q)
Console.WriteLine(i);
Depending on how you set those values, it will use zero, one or both of the where clauses.
The Dynamic LINQ string would need to look something like:
"Date >= DateTime(2015, 10, 21)"
This is mentioned in the documentation in the DynamicQuery project in the download mentioned at http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library.
Note, there isn't a new before the DateTime constructor.
I tried this and it works. I'm using Telerik's RadGrid control for ASP.NET AJAX. The grid builds the filter string and I needed to add the filter to my query to get the filter to execute in the database using LINQ to Entities. The problem is that the generated filter needed to be altered a little for it to work with LINQ to Entities as opposed to LINQ to Objects. It was doing a DateTime.Parse() which isn't supported in LINQ to Entities.