WP7 Sterling Database DateTime Index - c#

I am using a Sterling Database and have defined a class and index as follows
public class SingleEventDB :TimetableEventDB
{
public DateTime EventDateTime { get; set; }
}
CreateTableDefinition<SingleEventDB, string>(k => k.UniqueId)
.WithIndex<SingleEventDB,DateTime,string>("BYDATETIME",i=>i.EventDateTime)
I access this like this
public List<SingleEvent> GetAllSingleEvents(DateTime StartDate)
{
var allSingleEvents = new List<SingleEvent>();
var result =
from eveItemDB in App.MyDatabase.Query<SingleEventDB, DateTime, string>("BYDATETIME")
where (eveItemDB.Index >= StartDate)
select new SingleEvent
{
UniqueId = eveItemDB.LazyValue.Value.UniqueId,
NextDateTime = eveItemDB.Index,
Details = eveItemDB.LazyValue.Value.Details,
Location = eveItemDB.LazyValue.Value.Location
};
foreach (SingleEvent eveItem in result)
{
allSingleEvents.Add(eveItem);
}
return allSingleEvents;
}
However whilst the where clause is correctly filtering out the objects which are earlier than the input date, they are returned in the order that they were created, rather than in the index (DateTime) order.
Is this expected, or am I doing something wrong.
And is there a way to insert an orderby clause in t.

Seems you missed the Enumerable.ToList extension method! And yes, you could simply add orderby eveItemDB.Index to your code, like this:
public List<SingleEvent> GetAllSingleEvents(DateTime StartDate)
{
var result =
from eveItemDB in App.MyDatabase.Query<SingleEventDB, DateTime, string>("BYDATETIME")
where (eveItemDB.Index >= StartDate)
orderby eveItemDB.Index
select new SingleEvent
{
UniqueId = eveItemDB.LazyValue.Value.UniqueId,
NextDateTime = eveItemDB.Index,
Details = eveItemDB.LazyValue.Value.Details,
Location = eveItemDB.LazyValue.Value.Location
};
return result.ToList();
}

Related

Some values in LINQ Query Statement aren't saved correctly to class with subsonic 3

I am developing a MVC 3 Application which uses Subsonic 3 for accessing the database.
My Problem is, i don't understand why the Enum "GlobalType" is not being written into the property.
Everytime i check, the value is 0 instead of "One".
The "Name" property contains the "DateCreated" value.
The "DateCreated" property contains a new DateTime instance.
No other fields, as far as i'm aware of, are doing this.
There is no logic inside of the ViewItemModel, it's just a class with properties.
If i add them after this method manually, everything works.
Maybe someone encountered something similar with subsonic (if it even is subsonic itself, maybe i'm making a mistake)?
I have this method in the Backend:
public IEnumerable<ViewItemModel> LoadView(int registratorId)
{
var itemModel = from item in _itemQuery
join header in _headerQuery on item.HeaderID equals header.ID
where header.RegistratorID == registratorId && !(from hidden in _headerHiddenQuery where hidden.ItemID == item.ID && hidden.Type == GlobalType.One && hidden.RegistratorID == registratorId select hidden.ID).Any()
orderby item.ID descending
select new ViewItemModel()
{
Type = GlobalType.One,
ID = item.ID,
Name = header.Name,
DateCreated = header.DateCreated,
TypeOfTransport = header.TypeOfTransport,
TransportType = item.TransportType,
Count = (from subItems in _subItemQuery where subItems.ItemID == item.ID select subItems.ID).Count(),
// For Status
IsArchived = header.IsArchived,
IsCanceled = header.IsCanceled,
Process = header.Process,
End = header.End,
IsPublished = header.IsPublished,
OpenFrom = header.OpenFrom,
OpenTill = header.OpenTill,
IsNextStarted = header.IsNextStarted
};
return itemModel.ToList();
}
Update:
The GlobalType enum looks like this
public enum GlobalType
{
One = 1,
Two = 2,
Individual = 3
}
If i add them manually, i changed the return statement for this:
var result = itemModel.ToList();
foreach (var item in result)
{
var headerId = _itemQuery.Where(it => it.ID == item.ID).Select(it => it.HeaderID).FirstOrDefault();
var created = _itemQuery.Where(it => it.ID == item.ID).Select(it => it.DateCreated).FirstOrDefault();
var name = _headerQuery.Where(it => it.ID == headerId).Select(it => it.Name).FirstOrDefault();
item.AnnouncementType = GlobalType.One;
item.Name = name;
item.DateCreated = created;
}
return result;
try sample code:
public int enum GlobalType
{
One = 1,
Two = 2,
Individual = 3
}
//enum value Convert to int or other data type using casting
item.AnnouncementType = (int) GlobalType.One;
//Suppose if condition using
if((GlobalType)item.AnnouncementType==GlobalType.One)
{
//your code
}
Thanks to DaveParsons comment, i managed to create a workaround.
In this case, the code will have to iterate twice through the list of found elements, but won't load the entire table into memory.
Since there is a bug (throwing exception) with creating an anonymous object containing multiple classes like so:
select new { item, header, subItems }
I managed to get all the data needed, by manually assigning what i need like so:
public IEnumerable<ViewItemModel> LoadView(int registratorId)
{
var itemModel = from item in _itemQuery
join header in _headerQuery on item.AnnouncementHeaderID equals header.ID
where header.RegistratorID == registratorId && !(from hidden in _headerHiddenQuery where hidden.ItemID == item.ID && hidden.Type == GlobalType.One && hidden.RegistratorID == registratorId select hidden.ID).Any()
orderby item.ID descending
select new {
Type = GlobalType.One,
ID = item.ID,
Name = header.Name,
DateCreated = header.DateCreated,
TypeOfTransport = header.TypeOfTransport,
TransportType = item.TransportType,
Count = (from subItems in _subItemQuery where subItems.ItemID == item.ID select subItems.ID).Count(),
// For Status
IsArchived = header.IsArchived,
IsCanceled = header.IsCanceled,
Process = header.Process,
End = header.End,
IsPublished = header.IsPublished,
OpenFrom = header.OpenFrom,
OpenTill = header.OpenTill,
IsNextStarted = header.IsNextStarted
};
return itemModel
.ToList()
.Select(it => new ViewItemModel() {
Type = it.Type,
ID = it.ID,
Name = it.Name,
DateCreated = it.DateCreated,
TypeOfTransport = it.TypeOfTransport,
TransportType = it.TransportType,
Count = it.Count,
// For Status
IsArchived = it.IsArchived,
IsCanceled = it.IsCanceled,
Process = it.Process,
End = it.End,
IsPublished = it.IsPublished,
OpenFrom = it.OpenFrom,
OpenTill = it.OpenTill,
IsNextStarted = it.IsNextStarted
})
.ToList();
}
Notice: The return value of the query is an anonymous object with every single necessary field declared.
After the database returned all fields with the same name as in the database (model), we then have to force execution with ".ToList()" or something similar (deferred execution?).
Since the data is now in memory, we can assign the values from the anonymous object to the original class that was intended for this purpose.
I am sure there is a more reliable way using reflection, but this is what i have come up with.

Select single object from a query

I have the following query that pulls the data I need perfectly fine.
var subFuncName = from a in m_dcSQL_ConnectionProdTest.DC3_SubFunctions
where a.VersionIndex == versionIndex && stepDistinct.Select(b => b.Step).Contains(a.FunctionNumber) && stepDistinct.Select(c => c.LogID).Contains(a.SubFunctionNumber)
select new
{
a.FunctionNumber,
a.SubFunctionNumber,
a.SubFunctionName,
};
Then I want to add some data to a list.
foreach (var item in stepDistinct)
{
lstPareto.Add(new clsPareto(Convert.ToInt32(item.Step), Convert.ToInt32(item.LogID),
stepLogID.Where(p => p.Step.Equals(item.Step) && p.LogID.Equals(item.LogID)).Count(),
subFuncName.Where(x => x.FunctionNumber.Equals(item.Step) && x.SubFunctionNumber.Equals(item.LogID)).Select(x => x.SubFunctionName).ToString())); --THIS LINE IS THE PROBLEM--
}
My clsPareto class:
public class clsPareto
{
public int intStep { get; set; }
public int intLogID { get; set; }
public int iCount { get; set; }
public string strFuncName { get; set; }
public clsPareto(int ParetoStep, int ParetoLogID, int Count, string FuncName)
{
intStep = ParetoStep;
intLogID = ParetoLogID;
iCount = Count;
strFuncName = FuncName;
}
}
What I am trying to do, is to pull each SubFunctionName from subFuncName where FunctionNumber = Step and SubFunctionNumber = LogID. However, when I bind it to my datagrid, the column meant to show the names just shows the SQL Query String instead and doesn't actually take the elements I want. I thought my .Select(x => x.SubFunctionName) would do the trick, but apparently it doesn't. Still pretty new to using LINQ and C#, so how would I go about doing this?
The linq for the problem line is still an expression - Select() returns an IEnumerable not the value - and then you are doing a ToString() on it. This is why you just get SQL back.
You need to resolve the expression and get an actual object out of it. Adding Single() to get the FuncName should do it. You may also not need to convert to string if FuncName already is:
subFuncName.Where(x => x.FunctionNumber.Equals(item.Step) && x.SubFunctionNumber.Equals(item.LogID))
.Select(x => x.SubFunctionName).Single().ToString()));
This solution will throw an exception if there is no matching element in the collection.
subFuncName.Where(x => x.FunctionNumber.Equals(item.Step) && x.SubFunctionNumber.Equals(item.LogID))
.Select(x => x.SubFunctionName).Single().ToString()));
Could use;
var subFuncName = subFuncName.Where(x => x.FunctionNumber.Equals(item.Step) && x.SubFunctionNumber.Equals(item.LogID))
.Select(x => x.SubFunctionName).FirstOrDefault());
if(subFuncName != null)
// Add it
else
subFuncName == "UNDEFINED";
and handle the case if subFuncName is null yourself.

Is there a way to check the data in advance of runtime if it's not the correct type?

I occasionally get data that is not completely clean, and during runtime I get error messages because the data doesn't match the expected type. For example, sometimes the data has a string where there should be an int, or an int where there should be a date.
Is there a way to scan the data first for bad data, so that I can fix it all at once instead of finding out during run-time and fixing it iteratively?
Here's my code which works:
class TestScore{
public string Name;
public int Age;
public DateTime Date;
public DateTime Time;
public double Score;
}
//read data
var Data = File.ReadLines(FilePath).Select(line => line.Split('\t')).ToArray();
//select data
var query = from x in Data
select new { Name = x[3], Age = x[1], Date = x[2], Time = x[5], Score = x[7] };
//create List and put data into List
List<TestScore> Results = new List<TestScore>();
for (int i = 0; i < query.Count; i++)
{
TestScore TS = new TestScore();
TS.Name = query[i].Name;
TS.Age = query[i].Age;
TS.Date = query[i].Date;
TS.Time = query[i].Time;
TS.Score = query[i].Score;
Results.Add(TS);
}
Is there a way to scan the data first for bad data, so that I can fix
it all at once instead of finding out during run-time and fixing it
iteratively?
Scanning is a runtime operation. However, it's fairly straightforward to implement a solution that gives you enough information to "fix it all at once".
The following code shows a pattern for validating the file in its entirety, and doesn't attempt to load any data unless it completely succeeds.
If it fails, a collection of all errors encountered is returned.
internal sealed class ParseStatus
{
internal bool IsSuccess;
internal IReadOnlyList<string> Messages;
}
private ParseStatus Load()
{
string filePath = "foo";
var data = File.ReadLines( filePath ).Select( line => line.Split( '\t' ) ).ToArray();
var results = from x in data
select new { Name = x[3], Age = x[1], Date = x[2], Time = x[5], Score = x[7] };
var errors = new List<string>();
int row = 0;
// first pass: look for errors by testing each value
foreach( var line in results )
{
row++;
int dummy;
if( !int.TryParse( line.Age, out dummy ) )
{
errors.Add( "Age couldn't be parsed as an int on line " + row );
}
// etc...use exception-free checks on each property
}
if( errors.Count > 0 )
{
// quit, and return errors list
return new ParseStatus { IsSuccess = false, Messages = errors };
}
// otherwise, it is safe to load all rows
// TODO: second pass: load the data
return new ParseStatus { IsSuccess = true };
}
For not finding out the errors during run-time, the best thing that I can think of would be to correct the data manually before your program runs ..
But as we are trying do things constructive, I think that using a static readonly field to indicate the data error would be helpful. The following is a simple example which doesn't take the failed items, you might want to modify it when you are going to do some advanced handling.
public partial class TestScore {
public static TestScore Parse(String plainText) {
var strings=plainText.Split('\t');
var result=new TestScore();
if(
strings.Length<5
||
!double.TryParse(strings[4], out result.Score)
||
!DateTime.TryParse(strings[3], out result.Time)
||
!DateTime.TryParse(strings[2], out result.Date)
||
!int.TryParse(strings[1], out result.Age)
)
return TestScore.Error;
result.Name=strings[0];
return result;
}
public String Name;
public int Age;
public DateTime Date;
public DateTime Time;
public double Score;
public static readonly TestScore Error=new TestScore();
}
public static partial class TestClass {
public static void TestMethod() {
var path=#"some tab splitted file";
var lines=File.ReadAllLines(path);
var format=""
+"Name: {0}; Age: {1}; "
+"Date: {2:yyyy:MM:dd}; Time {3:hh:mm}; "
+"Score: {4}";
var list=(
from line in lines
where String.Empty!=line
let result=TestScore.Parse(line)
where TestScore.Error!=result
select result).ToList();
foreach(var item in list) {
Console.WriteLine(
format,
item.Name, item.Age, item.Date, item.Time, item.Score
);
}
}
}

How to clear default select clause on IQueryable resultset

If I have a page class that returns an IQueryable result set like:
protected virtual IQueryable<EntityResult> GetEntities(ETBDataContext pContext)
{
return from e in pContext.Entities
where e.SectionId == SectionId && e.StatusCode == "Published"
orderby e.PublishDate descending
select new EntityResult
{
EntityId = e.Id,
Excerpt = e.Excerpt,
Name = e.Name,
PublishDate = e.PublishDate,
ShortDescription = e.ShortDescription
};
}
If I call this method in a inherited class, How can I clear the select and just get the ShortDescription?
public void IQueryable<EntityResult> GetResult(ETBDataContext pContext)
{
IQueryable<EntityResult> pQuery = base.GetEntities(pContext);
//right here: how can I just return the ShortDescription Only?
return pQuery;
}
I am using the default GetEntities() to do the standard select operation for default queries, but on some calls I would like to get just the specific data that I need.
Is this possible? are there other ways? Thanks in advance!!
You can try
pQuery = pQuery.Select(e => new EntityResult {
ShortDescription = e.ShortDescription
});
I'm pretty sure that this won't select the other columns.

Sort ObservableCollection by Date

I have a collection
private ObservableCollection<ContentItemViewModel> _contentTree;
public ObservableCollection<ContentItemViewModel> ContentTree
{
get { return _contentTree; }
}
class ContentItemViewModel has property:
private string _published;
public string Published
{
get
{
return _published;
}
set
{
_published = value;
NotifyPropertyChanged("Published");
}
}
that is - node.Published= Convert.ToDateTime(date.Value).ToString("dd MMM yyyy", new DateTimeFormatInfo());
I need to sort ContentTree collection by date? how can I do this?
I do not know if there is any better way but you can do the following. The idea here is to create an ordered list and first foreach item in the ordered list, removing and re-adding the related item from content tree.
var tempList = _contentTree.OrderBy(p => DateTime.Parse(p.DateAndTime));
tempList.ToList().ForEach(q =>
{
_contentTree.Remove(q);
_contentTree.Add(q);
});
Or you can employ Comparison;
Comparison<ContentItemViewModel> comparison = new Comparison<ContentItemViewModel>(
(p,q) =>
{
DateTime first = DateTime.Parse(p.DateAndTime);
DateTime second = DateTime.Parse(q.DateAndTime);
if (first == second)
return 0;
if (first > second)
return 1;
return -1;
});
List<ContentItemViewModel> tempList = _contentTree.ToList();
tempList.Sort(comparison);
_contentTree = new ObservableCollection<ContentItemViewModel>(tempList);
you can use this:
ConceptItems = new ObservableCollection<DataConcept>(ConceptItems.OrderBy(i => i.DateColumn));
Change it to list ToList();
Sort it using OrderBy or...
Make a new ObservableCollection using that list.

Categories