I want to convert Gregorian date to Shamsi in Linq query
This is my query
Contex = new RfidReaderEntities1();
var selectall = from myreads in Contex.V_DriverWithReadTags select new { myreads.RfidTagId, myreads.CarType, myreads.Plate, myreads.VisiteDate, myreads.Lname,myreads.shamsidate};
dataGridView1.DataSource = selectall.ToList();
I want to convert myreads.VisiteDate to Shamsi date.
First download FarsiLibrary.Utils because it convert Gregorian to Shamsi without any mistake add it's DLL files to your project to more information go Here.
then try this:
var selectall = from myreads in Contex.V_DriverWithReadTags select new {
RfidTagId = myreads.RfidTagId,
CarType = myreads.CarType,
Plate = myreads.Plate,
VisiteDate = PersianDateConverter.ToPersianDate(myreads.VisiteDate),
Lname = myreads.Lname,
shamsidate = myreads.shamsidate
};
Contex = new RfidReaderEntities1();
var selectall = from myreads in Contex.V_DriverWithReadTags select new {
RfidTagId = myreads.RfidTagId,
CarType = myreads.CarType,
Plate = myreads.Plate,
VisiteDate = PersianDateConverter.ToPersianDate((DateTime)myreads.VisiteDate),
// VisiteDate = myshamsifun.GetShamsiDateTime((DateTime)myreads.VisiteDate),
Lname = myreads.Lname,
};
dataGridViewlisttoday.DataSource = selectall.ToList();
Related
I'm trying to load data from DynamoDB.
I use FilterExpression and KeyExpression.
If I search by simple value on the top level everything works fine.
However, when I try to filter records by nested map values, I get 0 records.
CurrentCase is an object, Assignments is Dictionary, Setup is Enum.
Here is my code:
`Expression filterExpression = new ();
filterExpression.ExpressionAttributeNames["#Setup"] = "CurrentCase.Assignments.Setup";
filterExpression.ExpressionAttributeValues[":userId"] = userId;
filterExpression.ExpressionStatement = "#Setup = :userId";`
I tried another way, didn't help. (WHERE CurrentCase.Assignments['Setup'] = 'Id' works in PartyQL):
`Expression filterExpression = new ();
filterExpression.ExpressionAttributeNames["#Setup"] = "CurrentCase.Assignments['Setup']";
filterExpression.ExpressionAttributeValues[":userId"] = userId;
filterExpression.ExpressionStatement = "#Setup = :userId";`
This is how i call query
var queryOperationConfig = new QueryOperationConfig
{
PaginationToken = paginationToken,
Limit = pageSize,
IndexName = GlobalIndexNames.Cases,
KeyExpression = keyExpression,
FilterExpression = filterExpression
};
Search search = _dbContext.GetTargetTable<CaseEntity>().Query(queryOperationConfig);
List<Document> documents = await search.GetNextSetAsync(cancellationToken);
I expect that this request return all records where CurrentCase.Assignments['Setup'] equals userId
Forgive me, im not a .Net coder, but your issue is this:
filterExpression.ExpressionAttributeNames["#Setup"] = "CurrentCase.Assignments.Setup";
You are essentially setting your var #Setup to a String "CurrentCase.Assignments.Setup"
It should be:
ExpressionAttributeNames = new Dictionary<string, string>
{
{ "#CurrentCase", "CurrentCase" },
{ "#Assignments", "Assignments" },
{ "#Setup", "Setup" }
}
filterExpression.ExpressionStatement = "#CurrentCase.#Assignments#Setup = :userId";`
You may need to restructure the example I gave, but you should get the idea.
my question is fairly simple, which method should I use and why for parsing XML files?
Right now I have function for that:
return new EdiFile
{
SPPLR_MAILBOX = xmlDoc.Element("SPPLR_MAILBOX").Value,
MESSAGE_ID = xmlDoc.Element("MESSAGE_ID").Value,
ATTRIBUTE05 = xmlDoc.Element("ATTRIBUTE05").Value,
Levels0 = (from a in xmlDoc.Element("LEVELS0").Elements("LEVEL0")
select new Level0
{
PLT_NUM = a.Element("PLT_NUM").Value,
PLT_LABEL_ID = a.Element("PLT_LABEL_ID").Value,
BOX_QTY = a.Element("BOX_QTY").Value,
PLT_WEIGHT = a.Element("PLT_WEIGTH").Value,
PLT_DIMENSION = a.Element("PLT_DIMENSION").Value,
PLT_CHEM = a.Element("PLT_CHEM").Value,
PLT_NOT_STACK = a.Element("PLT_NOT_STACK").Value,
PLT_NOTE = a.Element("PLT_NOTE").Value,
ATTRIBUTE01 = a.Element("ATTRIBUTE01").Value,
ATTRIBUTE02 = a.Element("ATTRIBUTE02").Value,
ATTRIBUTE03 = a.Element("ATTRIBUTE03").Value,
ATTRIBUTE04 = a.Element("ATTRIBUTE04").Value,
ATTRIBUTE05 = a.Element("ATTRIBUTE05").Value,
Levels1 = (from b in a.Element("LEVELS1").Elements("LEVEL1")
select new Level1
{
BOX_NUM = b.Element("BOX_NUM").Value,
BOX_LABEL_ID = b.Element("BOX_LABEL_ID").Value,
Items = (from c in b.Element("ITEMS").Elements("ITEM")
select new Item
{
SPPLR_ITEM = c.Element("SPPLR_ITEM").Value,
CUST_ITEM = c.Element("CUST_ITEM").Value,
ATTRIBUTE01 = c.Element("ATTRIBUTE01").Value,
ATTRIBUTE02 = c.Element("ATTRIBUTE02").Value,
ATTRIBUTE03 = c.Element("ATTRIBUTE03").Value,
ATTRIBUTE04 = c.Element("ATTRIBUTE04").Value,
ATTRIBUTE05 = c.Element("ATTRIBUTE05").Value,
Lots = (from d in c.Element("LOTS").Elements("LOT")
select new Lot
{
LOT_NUM = d.Element("LOT_NUM").Value,
LOT_LABEL_ID = d.Element("LOT_LABEL_ID").Value,
LOT_NOTE = d.Element("LOT_NOTE").Value,
LOT_EXP_DATE = d.Element("LOT_EXP_DATE").Value,
QTY = d.Element("QTY").Value,
UOM = d.Element("UOM").Value,
ATTRIBUTE01 = d.Element("ATTRIBUTE01").Value,
ATTRIBUTE02 = d.Element("ATTRIBUTE02").Value,
ATTRIBUTE03 = d.Element("ATTRIBUTE03").Value,
ATTRIBUTE04 = d.Element("ATTRIBUTE04").Value,
ATTRIBUTE05 = d.Element("ATTRIBUTE05").Value
}).ToList()
}).ToList()
}).ToList()
}).ToList()
};
But should I use deserialize? I learnt about serialization yesterday.
I cant really find any source explaining my method, vs deserialization.
Can someone get me any insight please?
Your method is more performance and flexie, because you have control on processing xml file. Serialization is using reflection and it is less performance.
var builder = new SqlBuilder();
var sqlQuery = #"Select * From xxxxTable /**where**/";
var template = builder.AddTemplate(sqlQuery);
builder.Where("xxx1= #xxx1", new { xxx1= xxx1});
builder.Where("convert(date,xxx2) < convert(date,GETDATE())"); // compare with sql server current date is Okay!
builder.Where("convert(date,xxx3) < convert(date,#xxx3)", new {xxx3 = xxx3}); // compare with parameter date is not okay!
using (var connection = Db.GetConnection(xx_ConnectionString_xx))
{
return connection.Query<xxModel>(template.RawSql,template.Parameters).FirstOrDefault();
}
Any idea for SqlBuilder usage for comparing datetime column value.
Thank you ....
I have a problem with converting linq result to object.
I have a class called Plant and a database which contains information about it (for example name, latin name, habitats etc).
I want to create a new object from executed query and send it to another part of application. So I'm messing with this code:
using (DataClassesDataContext dc = new DataClassesDataContext())
{
var sPlant = (from p in dc.Plants where p.Name == plantName select new Plant
{
Name = p.Name,
LatinName = p.LatinName,
Habitat = p.Habitat,
LeafHarvesting = p.LeafHarvesting,
FlowerHarvesting = p.FlowerHarvesting,
FruitHarvesting = p.FruitHarvesting,
RootHarvesting = p.RootHarvesting,
Morphology = p.Morphology,
Pharmacology = p.Pharmacology,
Img = p.Img,
GPSCoordinates = p.GPSCoordinates
}
);
But it doesn't convert result to a new Plant object.
As it seems that Plant is not part of the data store, you need to return an object that Linq to SQL can handle, to then create your Plant instance locally.
Start by querying for a list of anonymous objects containing the properties you need, and then only create your Plant. Add a First() or a FirstOrDefault() at the end to retrieve only one Plant:
using (DataClassesDataContext dc = new DataClassesDataContext())
{
var sPlant = (from p in dc.Plants where p.Name == plantName
select new {
Name = p.Name,
LatinName = p.LatinName,
Habitat = p.Habitat,
LeafHarvesting = p.LeafHarvesting,
FlowerHarvesting = p.FlowerHarvesting,
FruitHarvesting = p.FruitHarvesting,
RootHarvesting = p.RootHarvesting,
Morphology = p.Morphology,
Pharmacology = p.Pharmacology,
Img = p.Img,
GPSCoordinates = p.GPSCoordinates
}).AsEnumerable().Select(p => new Plant
{
Name = p.Name,
LatinName = p.LatinName,
Habitat = p.Habitat,
LeafHarvesting = p.LeafHarvesting,
FlowerHarvesting = p.FlowerHarvesting,
FruitHarvesting = p.FruitHarvesting,
RootHarvesting = p.RootHarvesting,
Morphology = p.Morphology,
Pharmacology = p.Pharmacology,
Img = p.Img,
GPSCoordinates = p.GPSCoordinates
}).First();
}
Use First or FirstOrDefault function to get the object. See here to get the difference.
If your query is suppose to return multiple results, use .TOList(). If you want to take first row only,use FirstOrDefault()
Is there anyway to read the specific line in this xml?
http://i.stack.imgur.com/hDPIg.jpg
var guide = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameTitle = (string)query.Element("Title"),
Gamedescription = (string)query.Element("Description"),
GameGuide = (string)query.Element("Guide")
};
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameGuide.StartsWith("See 'Payne In The Ass'")).Take(1);
this will show all guides in the xml.
This Work:
var guide = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameTitle = (string)query.Element("Title"),
Gamedescription = (string)query.Element("Description"),
GameGuide = (string)query.Element("Guide")
};
//GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameGuide.StartsWith("See 'Payne In The Ass'")).Take(1);
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameTitle.StartsWith("Serious"));
So start with whatever is the first child in the XML.
Just continue your statement with a where clause: (change the condition based on your needs)
var guides = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameGuide = (string)query.Element("Guide")
};
AchivementsListBox.ItemsSource = guides.Where( ngc => ngc.GameGuide.StartsWith("Chapter 4:"));