I would like to get two columns like SoccerStatus, SoccerDate from the SoccerAvailability in SQLite database into a list where SoccerDate="Today" using LINQ query ? I have tried the below query, but its throws exception, I need CurrentDate alone, no time is needed in my query.
List<SoccerAvailability> myList = (from x in conn.Table<SoccerAvailability>().Where(x => x.CurrentDate == DateTime.Now.ToString("ddMMyyyy")) select x).ToList();
Unhandled Exception:
SQLite.SQLiteException: no such function: tostring
convert the current date to a string before you execute your query
var currentDate = DateTime.Now.ToString("ddMMyyyy");
List<SoccerAvailability> myList = (from x in conn.Table<SoccerAvailability>().Where(x => x.CurrentDate == currentDate) select x).ToList();
Related
I would like to get FullName, Emailcolumn values using LINQ query in c# to a list and then get these values, but it giving below error;
Cannot implicity convert type Systems.Collections.Generic.List<<anoynymous type: string FullName>>
List <PlayerDetails> details = (from x in conn.Table<PlayerDetails>().Where(x => x.Email == emailTextVal).Select(p => new { p.FullName }) select x).ToList();
SoccerAvailability soccerAvailability = new SoccerAvailability();
soccerAvailability.FullName = details[0].FullName;
soccerAvailability.Email = emailTextVal;
why don't you just select the entire PlayerDetail record?
List <PlayerDetails> details = conn.Table<PlayerDetails>().Where(x => x.Email == emailTextVal).ToList();
SoccerAvailability soccerAvailability = new SoccerAvailability();
soccerAvailability.FullName = details[0].FullName;
soccerAvailability.Email = emailTextVal;
alternately, you really only need to select the FullName, since you already know the e-mail value
You should not mix up Lambda with C# Expression, i mean you can, but i wouldn't recommend it.
I think this is the query you need:
(from x in conn.Table<PlayerDetails>()
where x.Email == emailTextVal
select new {
Name = x.FullName,
Email = x.Email
}).ToList()
I'm trying to translate this SQL statement to LINQ:
SELECT sessionid, userid, CAST(sessiondate AS DATETIME) + CAST(sessiontime AS DATETIME) AS sessiondatetime FROM sometable
where sessiondate is of type DATE and sessiontime is of type TIME.
I've tried the following:
var query = from session in table
select new
{
session.Id,
session.UserId,
DateTime = session.Date + session.Time
};
where table is the return value of a GetTable<Session>() call on a DataContext instance and the Session class maps sessionid to Id, userid to UserId, sessiondate to Date (DateTime), and sessiontime to Time (TimeSpan).
The LINQ gets translated to this rather lengthy SQL statement:
SELECT [t0].[sessionid] AS [Id], [t0].[userid] AS [UserId], CONVERT(DateTime,DATEADD(HOUR, DATEPART(HOUR, [t0].[sessiontime]), CONVERT(DateTime,DATEADD(MINUTE, DATEPART(MINUTE, [t0].[sessiontime]), CONVERT(DateTime,DATEADD(SECOND, DATEPART(SECOND, [t0].[sessiontime]), CONVERT(DateTime,DATEADD(MILLISECOND, DATEPART(MILLISECOND, [t0].[sessiontime]), [t0].[sessiondate])))))))) AS [DateTime] FROM [sometable] AS [t0]
Unfortunately, when attempting to execute that statement, it tells me that "The datepart millisecond is not supported by date function dateadd for data type date." I'm guessing it's unhappy about the DATEADD call with milliseconds. Is there a way to fix this?
Edit: note that both session.Date and session.Time are nullable.
That unfortunate data structure makes the code unfortunately ugly:
var query = from session in table
select new
{
session.id,
session.userid,
combinedDate = new DateTime(
session.Date.Year, session.Date.Month, session.Date.Day,
session.Time.Hour, session.Time.Minute, session.Time.Second,
session.Time.Millisecond)
};
Hello I have to write in linq the sql query below:
Declare #OrgID int
Declare #OrgFinalID int
Set #OrgID = 91702 ---91703, 91702, 83279
select #OrgFinalID =
case
when ParentOrganisationId is null then ItemID
else ParentOrganisationId
end
from Organisations
where ItemID = #OrgID
I tried to write this but I am not on the right way, sorry but I am new with LINQ:
var OrgID=91207;
var OrgFinalID = from o in context.Organisations
where o.ItemID == OrgID
select new
{
o.ParentOrganisationId == null ? o.ItemID : o.ParentOrganisationId,
}
I have to put, with the LINQ expression, the value inside the variable OrgFinalID.
Looks like you are expecting this to be just a single number? Than you can call .Single() on your query, that basically returns the value itself:
var OrgFinalID = (from o in context.Organisations
where o.ItemID == OrgID
select new
{
ID = o.ParentOrganisationId == null ? o.ItemID : Convert.ToInt32(o.ParentOrganisationId),
}).Single().ID;
Also note call to Convert.ToInt32 which is supported by Linq to SQL and should help you avoid type casting problem.
Other options:
SingleOrDefault - if there could be single result, or no result at all (returns null in this case)
First - if you expect one or more results from the query
FirstOrDefault - similar to SingleOrDefault , returns null if no results came from the query
I have a DataTable named dt. It has two Columns named atype_code and module.
Now i need to query a module based on specific atype_code.
Here is the code i wrote but not working.
DataTable dt = GetATypeCodeList();
var accType = (from myAccType in dt.AsEnumerable()
where myAccType.Field<string>("atype_code") == aTypeCode.Trim()
select myAccType.Field<string>("module"));
acctype is a System.Data.EnumerableRowCollection<System.String>.
Since you've said that you
need to query a module based on specific atype_code
, i assume that you want only one module with the given atype_code.
Then you should either use Single/SingleOrDefault or First/FirstOrDefault.
String firstModule = dt.AsEnumerable()
.Where(r => r.Field<string>("atype_code") == aTypeCode.Trim())
.Select(r => r.Field<string>("module"))
.FirstOrDefault();
// or
String singleModule = dt.AsEnumerable()
.Where(r => r.Field<string>("atype_code") == aTypeCode.Trim())
.Select(r => r.Field<string>("module"))
.SingleOrDefault();
Enumerable.Single throws an exception if there is more than one matching element. That can be useful to validate the data.
Edit:
Here's the query-syntax:
IEnumerable<String> modules =
from myAccType in dt.AsEnumerable()
where myAccType.Field<string>("atype_code") == aTypeCode.Trim()
select myAccType.Field<string>("module");
String module = modules.FirstOrDefault(); // returns null if no matching modules were found
I try to display the Total sum() from a sqlStmt but all i got is [object, object], any idea how?
Thanks
private function displayAmountHeading():void {
sqlStmt = new SQLStatement();
sqlStmt.sqlConnection = sqlConn;
sqlStmt.text = "SELECT SUM(Amount) FROM MainTable";
sqlStmt.execute();
var result:Array = sqlStmt.getResult().data;
if (result != null) trace(result);
}
//return [object, object]
You're attempting to trace an array. If you want to see the first value in that array, try
SELECT SUM(Amount) as sum FROM MainTable
trace(result[0].sum);
The object,object is the string representation of the object, without a toString() method. Use trace(ObjectUtil.toString(result)); If your new to the flex sqlStatement you should also read this and this
for more information about the return type of the sqlStatement and how to access properties of the result object, when using aggregate functions such as SUM, where u should use aliases, such as SUM(Amount) as sumAmount to later access the property, like resultObject["sumAmount"] or resultObject.sumAmount