I have this Model that hold list months Jan - Dec. I want user to be set start day of the logs example 26 Dec to Jan 25 or Jan 1 - Jan 31
public class SalaryMonth
{
public int Id { get; set; }
public int StartMonth { get; set; }
public int StartDay { get; set; }
public int EndMonth { get; set; }
public int EndDay { get; set; }
public string Name { get; set; }
}
How do I get the SalaryMonth based on the date supplied and but be one month this my business logic which always return null
namespace Contracts.Attendances
{
public interface IAttendanceService
{
Task<SalaryMonth> GetASalaryMonthByDate(DateTime date);
}
public class AttendanceService : IAttendanceService
{
/.../
public async Task<SalaryMonth> GetASalaryMonthByDate(DateTime date) {
var day = date.Day;
var month = date.Month;
var result = await _context.SalaryMonths.
.FirstOrDefaultAsync(p => (p.StartMonth == month && p.StartDay >= day && p.StartDay <= day)
|| ( p.EndMonth == month && p.EndDay >= day && p.EndDay <= day));
return result;
}
}
}
Json from Database
{
"SalaryMonth": [
{
"Id": "3",
"StartMonth": "2",
"StartDay": "26",
"EndMonth": "3",
"EndDay": "25",
"Name": "March"
},
{
"Id": "4",
"StartMonth": "3",
"StartDay": "26",
"EndMonth": "4",
"EndDay": "25",
"Name": "April"
},
{
"Id": "5",
"StartMonth": "4",
"StartDay": "26",
"EndMonth": "5",
"EndDay": "25",
"Name": "May"
},
{
"Id": "6",
"StartMonth": "5",
"StartDay": "26",
"EndMonth": "6",
"EndDay": "25",
"Name": "June"
},
{
"Id": "7",
"StartMonth": "6",
"StartDay": "26",
"EndMonth": "7",
"EndDay": "25",
"Name": "July"
},
{
"Id": "8",
"StartMonth": "7",
"StartDay": "26",
"EndMonth": "8",
"EndDay": "25",
"Name": "August"
},
{
"Id": "9",
"StartMonth": "8",
"StartDay": "26",
"EndMonth": "9",
"EndDay": "25",
"Name": "September"
},
{
"Id": "10",
"StartMonth": "9",
"StartDay": "26",
"EndMonth": "10",
"EndDay": "25",
"Name": "October"
},
{
"Id": "11",
"StartMonth": "10",
"StartDay": "26",
"EndMonth": "11",
"EndDay": "25",
"Name": "November"
},
{
"Id": "12",
"StartMonth": "11",
"StartDay": "26",
"EndMonth": "12",
"EndDay": "25",
"Name": "December"
},
{
"Id": "2",
"StartMonth": "2",
"StartDay": "26",
"EndMonth": "2",
"EndDay": "25",
"Name": "February"
},
{
"Id": "1",
"StartMonth": "12",
"StartDay": "26",
"EndMonth": "1",
"EndDay": "25",
"Name": "January"
}
]
}
How to I query the record to allow for users to get Salary month from supplied date. Example user provides any date between 26/12/2019 to 26/01/2020 the result must January since that date is in Jan Month because Jan StartMonth is 12 and StartDate 26 and EndMonth is 1 and EndDate 25
So I think I know what you’re trying to do. And if I understand correctly then your logic is a little mixed up in your linq call.
Given any day, that day cannot be less than or equal to a date and greater than or equal to that same date unless it is the same date.
So you need to do:
C# Code:
.FirstOrDefaultAsync(p => (p.StartMonth == month && p.StartDay <= day) || ( p.EndMonth == month && p.EndDay >= day ));
Hope this helps.
Related
I am creating a simple Bingo game.
I stored the different number in a String Array
String[] number = new[] { "" };
if (lblLetter.Text == "B")
{
//number = new[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" };
}
if (lblLetter.Text == "I")
{
//number = new[] { "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30" };
}
if (lblLetter.Text == "N")
{
//number = new[] { "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45" };
}
if (lblLetter.Text == "G")
{
//number = new[] { "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60" };
}
if (lblLetter.Text == "O")
{
number = new[] { "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75" };
}
I used a List Array to store the numbers that was already selected on the bingo thus should be removed on the String Array
public List<string> exceptions = new List<string>();
Appending the label Value to it
exceptions.Add(lblNumber.Text);
Now I want to remove specific Items on String Array that is found on the exceptions List String
I'm confused on how to do this, Any help will be appreciated.
You can use Except.
number = number.Except(exceptions).ToArray();
I am sending request data like this
{
"1": "a",
"2": "b",
"3": "c",
"4": "d",
"5": "e",
"array": [
{
"GroupID": "NUMBER",
"CodeID": "ONE",
"Value": "1"
},
{
"GroupID": "NUMBER",
"CodeID": "TWO",
"Value": "1"
},
{
"GroupID": "NUMBER",
"CodeID": "THREE",
"Value": "1"
},
{
"GroupID": "NUMBER",
"CodeID": "FOUR",
"Value": "1"
}
]
}
Response data is received through the API like this
{
"Code": "00",
"Msg": "SUCCESS",
"Data": [
{
"a": "1",
"b": "2",
"c": "3",
"d": "4",
"e": "5",
"f": "6",
"g": "7",
"h": "8",
"array": [
{
"GroupID": "NUMBER",
"CodeID": "ONE",
"Value": "1"
},
{
"GroupID": "NUMBER",
"CodeID": "TWO",
"Value": "1"
},
{
"GroupID": "NUMBER",
"CodeID": "THREE",
"Value": "1"
},
{
"GroupID": "NUMBER",
"CodeID": "FOUR",
"Value": "1"
},
{
"GroupID": "NUMBER",
"CodeID": "FIVE",
"Value": "0"
},
{
"GroupID": "NUMBER",
"CodeID": "SIX",
"Value": "0"
}
]
},
{
"a": "9",
"b": "10",
"c": "11",
"d": "12",
"e": "13",
"f": "14",
"g": "15",
"h": "16",
"agreements": [
{
"GroupID": "NUMBER",
"CodeID": "ONE",
"Value": "1"
},
{
"GroupID": "NUMBER",
"CodeID": "TWO",
"Value": "1"
},
{
"GroupID": "NUMBER",
"CodeID": "THREE",
"Value": "1"
},
{
"GroupID": "NUMBER",
"CodeID": "FOUR",
"Value": "0"
},
{
"GroupID": "NUMBER",
"CodeID": "FIVE",
"Value": "0"
},
{
"GroupID": "NUMBER",
"CodeID": "SIX",
"Value": "0"
}
]
}
]
}
I wrote a code like this that compares data and data and selects only the data that matches the requested data.
I put all the response data in the list and wrote the code like this
List<user_model> list= new List<user_model>();
List<user_model> list2= new List<user_model>();
foreach (user_model um in list)
{
if (um.array[0].Value == param["7"].ToString() &&
um.array[1].Value == param["8"].ToString() &&
um.array[2].Value == param["9"].ToString() &&
um.array[3].Value == param["10"].ToString())
{
list2.Add(bm);
}
}
Is there a way to code my code more concisely through lambda expressions?
Assuming bm is a typo you could do something like
list2.AddRange(
list.Where( um =>
um.array[0].Value == param["7"].ToString() &&
um.array[1].Value == param["8"].ToString() &&
um.array[2].Value == param["9"].ToString() &&
um.array[3].Value == param["10"].ToString()
)
);
If your values are arrays and not strings then you need to do an array compare, how you do this is up to you, e.g.
um.array[0].Except(param["7"]).Count() > 0
um.array[0].SequenceEquals( param["7"] )
there may be a point where it makes sense to pull this comparision logic out into a separate comparer class. There will be existing answers detailing that
I have two arrays variables and values like below
arraydata1 =
[
{
"id": "1",
"name": "aaa"
},
{
"id": "2",
"name": "bbb"
},
{
"id": "3",
"name": "ccc"
},
{
"id": "4",
"name": "ddd"
},
{
"id": "12",
"name": "aaa"
}
]
and
arraydata2 =
[
{
"id": "111",
"tablename": "aaa"
},
{
"id": "222",
"tablename": "bbb"
}
]
I want to compare arraydata1.name == arraydata2.tablename and if matching then form new array from arraydata1 .
output is -
[
{
"id": "1",
"name": "aaa"
},
{
"id": "2",
"name": "bbb"
},
{
"id": "12",
"name": "aaa"
}
]
I have more than 2000+ records to compare in arraydata1 how to reduce time as well. I can use normal foreach but it will take too much time to compare.
I was doing inside logic app using 2 foreach so it is taking time. so i thought better to use c# code.
One Linq solution could look like this:
var tableNameKeys = arraydata2.Select(t => t.tablename).ToHashSet();
var resultArray = arraydata1.Where(x => tableNameKeys.Contains(x.name)).ToArray();
The advantage of this approach is that HashSet.Contains
... is an O(1) operation.
Result:
I want to write a simple application that takes data from a database and formats it to a Json file. The catch is, that the views from which I'm getting the data are supposed to be changeable.
That means the Json can't be serialized from a rootclass. Also, I need to make sure if the tables have a parent/child connection this is depicted as well.
In the code below I have created a dataset to give you an idea of what I mean.
static void Main(string[] args)
{
DataSet dsSet = new DataSet("OrderManagement");
DataTable tCustumer = new DataTable("Custumer");
DataTable tOrder = new DataTable("Order");
tCustumer.Columns.Add("CustumerId");
tCustumer.Columns.Add("Name");
tOrder.Columns.Add("OrderId");
tOrder.Columns.Add("CustumerId");
tOrder.Columns.Add("Article");
tCustumer.Rows.Add("1", "Chris");
tCustumer.Rows.Add("2", "Ronja");
tCustumer.Rows.Add("3", "Thomas");
tOrder.Rows.Add("1", "1", "chocolate");
tOrder.Rows.Add("2", "1", "apples");
tOrder.Rows.Add("3", "2", "dogfood");
tOrder.Rows.Add("4", "3", "keyboard");
tOrder.Rows.Add("4", "3", "tomatos");
tOrder.Rows.Add("4", "3", "green tea");
dsSet.Tables.Add(tCustumer);
dsSet.Tables.Add(tOrder);
dsSet.Relations.Add(
"RelationCustumerOrder",
dsSet.Tables["Custumer"].Columns["CustumerId"],
dsSet.Tables["Order"].Columns["CustumerId"], false
);
dsSet.AcceptChanges();
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.All;
string text = JsonConvert.SerializeObject(dsSet, Formatting.Indented, settings);
}
This is the Json I want it to output:
{"Custumer": [
{
"CustumerId": "1",
"Name": "Chris"
"Order": [
{
"OrderId": "1",
"CustumerId": "1",
"Article": "chocolate"
},
{
"OrderId": "2",
"CustumerId": "1",
"Article": "apples"
},
]
},
{
"CustumerId": "2",
"Name": "Ronja"
"Order": [
{
"OrderId": "3",
"CustumerId": "2",
"Article": "dogfood"
}
]
},
{
"CustumerId": "3",
"Name": "Thomas"
"Order": [
{
"OrderId": "4",
"CustumerId": "3",
"Article": "keyboard"
},
{
"OrderId": "4",
"CustumerId": "3",
"Article": "tomatos"
},
{
"OrderId": "4",
"CustumerId": "3",
"Article": "green tea"
}
]
}],}
This is what I do get:
{"Custumer": [
{
"CustumerId": "1",
"Name": "Chris"
},
{
"CustumerId": "2",
"Name": "Ronja"
},
{
"CustumerId": "3",
"Name": "Thomas"
}],
"Order": [
{
"OrderId": "1",
"CustumerId": "1",
"Article": "chocolate"
},
{
"OrderId": "2",
"CustumerId": "1",
"Article": "apples"
},
{
"OrderId": "3",
"CustumerId": "2",
"Article": "dogfood"
},
{
"OrderId": "4",
"CustumerId": "3",
"Article": "keyboard"
},
{
"OrderId": "4",
"CustumerId": "3",
"Article": "tomatos"
},
{
"OrderId": "4",
"CustumerId": "3",
"Article": "green tea"
}]}
You can do this in couple of steps.
Step 1 : Set Relation.Nested Property as True.
dsSet.Relations.Add(
"RelationCustumerOrder",
dsSet.Tables["Custumer"].Columns["CustumerId"],
dsSet.Tables["Order"].Columns["CustumerId"]
);
dsSet.Relations[0].Nested = true;
Step 2 : Convert to Xml.
StringWriter sw = new StringWriter();
dsSet.WriteXml(sw);
string xmlString = sw.ToString();
Step 3: Serialize as Json for final result
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.All;
string jsonResult = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented);
Final Output for the sample would be
{
"OrderManagement": {
"Custumer": [
{
"CustumerId": "1",
"Name": "Chris",
"Order": [
{
"OrderId": "1",
"CustumerId": "1",
"Article": "chocolate"
},
{
"OrderId": "2",
"CustumerId": "1",
"Article": "apples"
}
]
},
{
"CustumerId": "2",
"Name": "Ronja",
"Order": {
"OrderId": "3",
"CustumerId": "2",
"Article": "dogfood"
}
},
{
"CustumerId": "3",
"Name": "Thomas",
"Order": [
{
"OrderId": "4",
"CustumerId": "3",
"Article": "keyboard"
},
{
"OrderId": "4",
"CustumerId": "3",
"Article": "tomatos"
},
{
"OrderId": "4",
"CustumerId": "3",
"Article": "green tea"
}
]
}
]
}
}
In my c# project I use Json.net Library.
I have long Json with many subfields, for ex:
{
"count": 10,
"Foo1": [
{
"id": "1",
"name": "Name1"
},
{
"id": "2",
"name": "Name3"
},
{
"id": "3",
"name": "Name4"
}
],
"Foo2": [
{
"id": "4",
"name": "Name3",
"specific_field": "specific_values1"
},
{
"id": "5",
"name": "Name3",
"specific_field": "specific_values2"
},
{
"id": "6",
"name": "Name3",
"specific_field": "specific_values3"
}
],
"Foo3": [
{
"id": "7"
},
{
"id": "8"
},
{
"id": "9"
}
]
}
And I need to get List of all specific_field (id 4-6), but cant deserialized json to object, because Foo1, Foo2 ... changed dynamically.
I want to know, is this possible to get values of specific_field when i have only json?
I think, I found solution:
var list = new List<string>();
var result = ((JToken)json);
foreach (var res in result)
{
list.AddRange(from foo in res.First let ret = foo["specific_field"] where (dynamic) ret != null select foo["specific_field"].ToString());
}
In comment, provide, what do you think about it?
You could use dynamics:
string json = "your JSON string comes here";
dynamic deserializedValue = JsonConvert.DeserializeObject(json);
var values = deserializedValue["Foo2"];
for (int i = 0; i < values.Count; i++)
{
Console.WriteLine(values[i]["specific_field"]);
}