Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have three classes :
public class QData
{
public List<RData> Items { get; set; }
public List<QDay> Dates { get; set; }
}
public class QDay
{
public string Date { get; set; }
public List<RData> Details { get; set; }
}
public class RData
{
public string Name { get; set; }
public int Quantity { get; set; }
}
my list is
List<QData> myList;
What is the most effective way to sort the list (*QData type) by Date, the Date is string.
Perhaps this is what you need:
var result = myList.Select(qData => new QData()
{
Items = qData.Items,
Dates = qData.Dates.OrderBy(qDay => DateTime.Parse(qDay.Date)).ToList();
}).ToList();
With DateTime.Parse call being perhaps modified to fit to the date format in the qDay.Date property.
Here is an example that sort using the first date in the Dates list. I can't imagine why you would ever want to do this but here it is. I suspect that having Dates be a list is a mistake, in fact you only want one date there.
var sortedList = MyList.OrderBy(element => DateTime.Parse(element.Dates.First().Date));
I think this is what you actually want... ONLY ONE LIST:
public class QData
{
RData itemInfo { get; set;}
QDay dateInfo { get; set; }
}
Then your sort would look like this:
var sortedList = MyList.OrderBy(element => DateTime.Parse(element.dateInfo.Date));
var temp = (from e in myList.Dates
orderby DateTime.Parse(e.Date)
select e
).ToList();
myList.Dates = temp;
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I'm using a dictionary in C# and want to make the value a custom class. I have the following code.
public class myMovies
{
public string Name { get; set; }
public string Year { get; set; }
}
Dictionary<string, myMovies> file_dict = new Dictionary<string, myMovies>();
foreach (string file in Directory.GetFiles(path1, "*.mkv", SearchOption.AllDirectories))
{
file_dict.Add(file, new myMovies("x", "x");
}
I'm doing something wrong, I just have no idea at this point. As a side note, this code does work when I just use a <string,string> dictionary and just store a text value in the value pair.
Edit
Required a constructor in my class definition. Thanks for help.
Either provide an appropriate constructor in the class definition:
public class myMovies
{
public string Name { get; set; }
public string Year { get; set; }
public myMovies(string name, string year)
{
Name = name;
Year = year;
}
}
Or use object initializer syntax to assign the property values when instantiating the object:
file_dict.Add(file, new myMovies { Name = "x", Year = "x" });
It's telling you it expects a Constructor, so give it what it expects:
public class myMovies
{
public myMovies(string name, string year)
{
Name = name;
Year = year;
}
public string Name { get; set; }
public string Year { get; set; }
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to get the vehicle type according to vehicle number. I need to select specific column from a list according to another column.
Here is my code:
protected void ddVehicleNo_SelectedIndexChanged(object sender, EventArgs e)
{
List<Exp_VehicleDTO> odata = (List<Exp_VehicleDTO>)Session["VehicleDTO"];
var vehityeps=odata.Select(x=>x.VehicleNo.Contains(x.VehicleType.Equals(Convert.ToInt32(ddVehicleNo.SelectedValue))))
}
This code causes error "the best overload method match for "string.contains(string)" has some invalid arguments".
Exp_vehicleDTO class
public class Exp_VehicleDTO
{
public int CompanyId { get; set; }
public int VehicleId { get; set; }
public int VehicleType { get; set; }
public string VehicleNo { get; set; }
public int Status { get; set; }
public DateTime CreatedDateTime { get; set; }
public string CreatedBy { get; set; }
public string CreatedMachine { get; set; }
}
You can get the vehicle type like so:
int vehicleType = odata.Single(x => x.VehicleNo.Equals(ddVehicleNo.SelectedValue)).VehicleType;
Single will take the first item that matches the condition. Note that it will throw an exception if the item isn't found, or if there are multiple matching items.
If you want to handle the case that the item isn't found, you can do something like this:
var vehicle = odata.SingleOrDefault(x => x.VehicleNo.Equals(ddVehicleNo.SelectedValue));
if (vehicle != null)
{
var vehicleType = vehicle.VehicleType;
}
else
{
// set combobox's SelectedIndex to -1
}
Difficult to help without knowing what error you are receiving but try changing your code to this:
protected void ddVehicleNo_SelectedIndexChanged(object sender, EventArgs e)
{
List<Exp_VehicleDTO> odata = (List<Exp_VehicleDTO>)Session["VehicleDTO"];
var vehityeps = odata
.Where(v => v.VehicleNo.ToString() == ddVehicleNo.SelectedValue)
.Select(v => v.VehicleType);
}
That should populate vehityeps with all VehicleType's where VehicleNo equals what the user has selected in the ddVehicleNo drop down.
UPDATED
I'm not sure what types are used Exp_VehicleDTO but my guess is VehicleNo is an int. To be safe with my solution this compares the values as strings.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to access referenced classes inside a main class and insert values into the objects. The classes are of partial type.
my code:
public partial class Get_CountryInfo_Resp_object
{
public string ReturnCode { get; set; }
public string ErrorMsg { get; set; }
public string Alpha2_Code { get; set; }
public string Digit3_Code { get; set; }
public string CountryName { get; set; }
public string IBAN_Mandatory { get; set; }
public As_SenderCountry[] As_SenderCountry { get; set; }
public As_ReceiverCountry[] As_ReceiverCountry { get; set; }
}
public partial class As_SenderCountry
{
public string SenderCountry_IsSensitive { get; set; }
}
public partial class As_ReceiverCountry
{
public string ReceiverCtry_EFTNotAllowed { get; set; }
public ReceiverCtry_AllowedCCY_Item[] ReceiverCtry_AllowedCCY_List { get; set; }
}
public partial class ReceiverCtry_AllowedCCY_Item
{
public string ReceiverCtry_AllowedCCY { get; set; }
}
private static void Task2()
{
String xmlText = File.ReadAllText(#"../../XML/sample1.xml");
DataSet ds = new DataSet();
ds.ReadXml(new XmlTextReader(new StringReader(xmlText)));
DataTable dt = ds.Tables["column"];
Get_CountryInfo_Resp_object Get_CountryInfo_Resp = new Get_CountryInfo_Resp_object();
//Get_CountryInfo_Resp.As_SenderCountry;
Get_CountryInfo_Resp.ReturnCode = dt.Rows[0]["column_Text"].ToString();
Get_CountryInfo_Resp.ErrorMsg = dt.Rows[1]["column_Text"].ToString();
Get_CountryInfo_Resp.Alpha2_Code = dt.Rows[2]["column_Text"].ToString();
Get_CountryInfo_Resp.Digit3_Code = dt.Rows[3]["column_Text"].ToString();
Get_CountryInfo_Resp.CountryName = dt.Rows[4]["column_Text"].ToString();
Get_CountryInfo_Resp.IBAN_Mandatory = dt.Rows[5]["column_Text"].ToString();
//GetCountryInfo_Resp.As_SenderCountry.SenderCountry_IsSensitive
I need to Insert dt.Rows[6]["column_Text"].ToString(); into the GetCountryInfo_Resp.As_SenderCountry.SenderCountry_IsSensitive .
How shall i proceed?
Please help.
Since As_SenderCountry is an array, it can contain multiple items. You have to assign an array too, not just a single instance.
I would start to create an object, add that to a list and eventually create an array out of it (or change the type to be a list instead of an array). You can also fix-size the array if you know the length already.
As_SenderCountry asc = new As_SenderCountry();
asc.SenderCountry_IsSensitive = dt.Rows[6]["column_Text"].ToString();
And then:
GetCountryInfo_Resp.As_SenderCountry = new As_SenderCountry[] { asc };
Or create the list, loop over items and eventually assign it:
List<As_SenderCountry> list = new List<As_SenderCountry>();
// some sort of loop
As_SenderCountry asc = new As_SenderCountry();
...
list.Add(asc);
// end loop
GetCountryInfo_Resp.As_SenderCountry = list.ToArray();
I don't think I fully understand your code, but As_SenderCountry and As_ReceiverCountry in your public partial class Get_CountryInfo_Resp_object are arrays, if I am not reading it wrong.
Therefore, the messy solution, if you know there is only one sender country:
GetCountryInfo_Resp.As_SenderCountry[0].SenderCountry_IsSensitive = dt.Rows[6]["column_Text"].ToString();
Alternatively, you can use Lists - the advantage with lists being, you don't need to know the array size when instantiating. An example with your variables:
public partial class Get_CountryInfo_Resp_object
{
public string ReturnCode { get; set; }
...
public List<As_SenderCountry> As_SenderCountry { get; set; }
public List<As_ReceiverCountry> As_ReceiverCountry { get; set; }
}
private static void Task2()
{
String xmlText = File.ReadAllText(#"../../XML/sample1.xml");
DataSet ds = new DataSet();
ds.ReadXml(new XmlTextReader(new StringReader(xmlText)));
DataTable dt = ds.Tables["column"];
Get_CountryInfo_Resp_object Get_CountryInfo_Resp = new Get_CountryInfo_Resp_object();
...
GetCountryInfo_Resp.As_SenderCountry.SenderCountry_IsSensitive.Add(dt.Rows[6]["column_Text"].ToString());
p.s. Your variable and class naming is very messy. I would suggest you to clean that up so that you, as well as the people reading the question can understand it better.
You need to create a new instance of the inner class as you would do normally for any other class, and then assign whatever value to the field you need.
First you need to assign the length of the array, then each cell of the array will contain an instance of an object of type (As_SenderCountry ), then you should assign each object the value you need.
GetCountryInfo_Resp.As_SenderCountry = new GetCountryInfo_Resp.As_SenderCountry();
GetCountryInfo_Resp.As_SenderCountry[INDEX_HERE].SenderCountry_IsSensitive = dt.Rows[6]["column_Text"].ToString();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Platform: C#
IDE: Visual Studio 2010
I am new to Linq and trying to filter a Json object using it. Any suggestions?
Here is the code:
string jsonGetData = objstats.getData_Emp(lblEmp.Text,lblYear.Text);
//JavaScriptSerializer strJsonSer = new JavaScriptSerializer();
var lstEmpAnalysis = JsonConvert.DeserializeObject<empAnalysis>(jsonGetData);
Now, from the above lstEmpAnalysis, I need to filter data on first table of the class empAnalysis where one of its index contains countries and show only those countries data being used in filter which is shown below :
public class empAnalysis
{
public List<List<object>> Table { get; set; }
public List<List<double>> Table1 { get; set; }
public List<List<object>> Table2 { get; set; }
}
So, any suggestions?
Input comes in this way :
Table
[0][0] : Abc
[0][1] : India
[0][2] : Engineer
[1][0] : Xyz
[1][1] : UK
[1][2] : Support Engineer
And what I want to filter is only the data which contains UK.
If you always know that second parameter of your table is the country you can do it like this :
var items= from item in empAnalysis.Table where item[1]=="UK" select item;
Although I always prefer to work with a strong typed object here as I mentioned in my comment :
For example :
public class Employee{
public string Name{get;set;}
public string Country{get;set;}
public string JobTitle{get;set;}
}
and
public class empAnalysis
{
public List<Employee> Table { get; set; }
public List<List<double>> Table1 { get; set; }
public List<List<object>> Table2 { get; set; }
}
then we could write :
var items= from employee in empAnalysis.Table where employee.Country=="UK" select item;
BTW for the purpose of clarity we can do something like this either :
var people=from item in empAnalysis.Table select new {
Title=item[0],
Country=item[1],
JobTitle=item[2],
};
var peopleFromUK=from person in people where person.Country=="UK";
Although you should be advised that you are getting a list of anonymous typed objects that have Title,Country and JobTitle properties.
UPDATE:
Here's a test that I wrote using NUnit and It passes.
[TestFixture]
public class LinqToObjectTests
{
[Test]
public void ThereShouldBeOnlyOneListForUK()
{
var list = new List<List<object>>
{
new List<object>(),
new List<object>()
};
list[0].Add("Name");
list[0].Add("UK");
list[0].Add("Title");
list[1].Add("Name");
list[1].Add("NOT UK");
list[1].Add("Title");
var query = from item in list where item[1] == "UK" select item;
Assert.AreEqual(1, query.Count());
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
my task is to group the data from a list of Subscription object which has the following structure:
public class Subscription
{
public string ApplicationId { get; private set; }
public string UserCode { get; private set; }
public string SubscriptionId { get; private set; }
public string SubscriptionCode { get; private set; }
public DateTime? StartDate { get; private set; }
public DateTime? EndDate { get; private set; }
}
The code return a list of subscription, so List. In my case I've several items with same ApplicationId but different StartDate and EndDate. Basically I have to group these items
in order to create a json structure like this:
"applicationId" : {
"subscriptions" : [
{
"startdate" : ...,
"enddate" : ...,
},
{
"startdate" : ...,
"enddate" : ...,
},
]
}
Can I achieve this using LINQ?
Try this:
subscriptions.GroupBy(item => item.SubscriptionCode)
.Select(group => new { Subscription = group.Key, Items = group.Select(item => new { StartDate = item.StartDate, EndDate = item.EndDate}) })
Not sure if it will work but it reads like it will.
subscriptions is just a list of your object.