I want to retrieve all tickets whose badge number is present in the list
i have a listA
public class Li
{
public string s { get; set; }
public string t { get; set; }
public string b { get; set; }
}
i have a method
i try this condition but it doesn't work,
doesn't recognize tostring for the first and doesn't recognize the list
Any idea how solve my problem?
This should work:
where listeLigneXls.Select(lgn => lgn.badge).Contains(chb.NUM_BADGE)
Need to change in this code block
where listeLigneXls.Select(lgn =>lgn.badge).Contains(chb.NUM_BADGE)
Contain is extension method of list of primitive data type
I change the method I get the values of the badge number and I would like to use it in my second query but I do not know how
Related
Hi I'm having difficulty finding an answer to my question here, so I figured I'd just ask. I have to lists of classes, ServiceItem, and ServiceDetailsClass. I want to filter out all of the ServiceDetailClass Items that are not int ServiceItems list. Here are the two classes:
public class ServiceItem
{
public long ID { get; set; }
public string Status { get; set; }
}
public class ServiceDetailsClass
{
public string Name;
public long ID;
public int Quantity;
public string Notes;
public string Status;
public string Description;
public DateTime CreatedDate;
}
So far the only things I've found on here is for lists that have a list in them, so this is a bit different. This is all I was able to come up with, but the filter list has 0 item, even though I know it should have more than that:
lstFilteredServiceDetailsClass = lstServiceDetailsClass.Where(i => lstServiceItem.Contains
(new ServiceItem { lngId = i.ServiceID, strStatus = "Locked" })
Any help is appreciated. Thanks!
You're making a new object and then checking the list to see if that exact object/instance is in it (i.e. because it's an object, it's comparing the reference).
Instead, you need to look for overlapping IDs.
Something like this should work:
List<ServiceItem> serviceItems;
List<ServiceItemDetails> serviceItemDetails;
var result = serviceItemDetails.Where(sid => serviceItems.Any(si => si.ID == sid.ID))
In English: "The collection of ServiceItemDetails where the list of service items has an item with the same ID"
I have following classes:
public class Selections
{
public List<Selection> selection { get; set; }
}
public class Selection
{
public Promotion promotion { get; set; }
public Products products { get; set; }
}
public class Products
{
public List<int> productId { get; set; }
}
I am creating List and assigning property values but when I am adding the list I'm getting error:
The best overloaded method match for
'System.Collections.Generic.List.Add(Selection)' has some
invalid arguments
C# code:
Selections productSelections = new Selections();
List<Selection> listOfProductSelections = new List<Selection>();
Selection dataSelection = new Selection()
{
promotion = new ProviderModels.Common.Promotion()
{
promotionID = Convert.ToInt32(applicablePromotion.PromotionId),
lineOfBusiness = applicablePromotion.LineOfBusiness
},
products = new ProviderModels.Common.Products()
{
productId = GetIdsOfSelectedProducts(context, selectedOffer)
}
};
productSelections.selection.Add(listOfProductSelections);
Am I missing something?
You are adding a list to another list. You want to add the list items.
Instead of
productSelections.selection.Add(listOfProductSelections);
write
productSelections.selection.AddRange(listOfProductSelections);
But you have to be sure you have initialized the selection property at that point, otherwise you'll run into a NullReferenceException.
By the way, check all your error messages. You will see a second message telling you which type is excpected and what you were using.
you should use AddRange as listOfProductSelections is a list.
productSelections.selection.AddRange(listOfProductSelections)
productSelections.selection is a reference to a List, consquently when you try to add an item to it (last line of your example) the Add method expects a parameter of type of Selection - you're passing listOfProductSelections which is a reference to another list.
Maybe you wanted to add dataSelection which is of the required type? If not, you can use AddRange as the other respondents have suggested.
I'm doing a query and get as result something like this, which i put into a Hashtable
{"success":"true", "result":[{"type":"email", "address":"aaasd#asd.com"},{"type":"email", "address":"aaasddee#dse.com"}]}
then i do
return hashtable["result"];
so I only have this left
[{"type":"email", "address":"aaasd#asd.com"},{"type":"email", "address":"aaasddee#dse.com"}]
but my problem is that I don't know how to iterate through every object from "result" to fill my own objects. I was searching for a solution but the only answer I found was to use
foreach(DictionaryEntry entry in searchResult) {
//do something<br>
}
When I iterate through the Hashtable like this I can only use the properties entry.Key and entry.Value but I can't say which value for a specific key I need. Any suggestions are welcome.
You can get it using deserialising using JSON.NET as shown below :-
var result = JsonConvert.DeserializeObject<dynamic>(searchResult);
You can create your class like below :-
public class RootObject
{
public string type { get; set; }
public string address { get; set; }
}
For more information :-
http://james.newtonking.com/json/help/index.html?topic=html/SerializingJSON.htm
Create a class that matches the signature of the result collection like:
public class Result
{
public string Type { get; set; }
public string Address { get; set; }
}
Then use Json.NET to parse the result node into a List<Result>. There is plenty of documentation online on how to use the Json.NET library.
Hope that helps,
Rob
I have a class definition that looks like this:
public class MyObjectModel
{
public int ObjectID { get; set; }
//for when the user's data is split in 2 fields
public string FirstName { get; set; }
public string LastName { get; set; }
//for when the user's data is all in one field
public string FirstLastName { get; set; }
}
I have a list of these MyObjectModel and I want to sort them by name with a custom sort process because that involves checking if the data contains a LastName (in this case sort using the LastName) or just FirstLastName (in this case I'm going to break the string and sort on the second term, if there's one, or just the whole string if there's only one word.)
I'm not sure on two things:
should I use IComparer or IComparable?
Once it determines the order of the sort (I can do that), how do I make it so that the output of the method is a list of ints representing ObjectID.
Use Linq:
List<MyObjectModel> objects = new List<MyObjectModel>();
List<int> ids = objects.OrderBy(o => FunctionWhichReturnsNameForSort(o)).Select(o => o.ObjectID).ToList();
FunctionWhichReturnsNameForSort can be implemented in another class, or an extension, or as a member.
// should be in a static helper class
public static string NameForSort(this MyObjectModel obj)
{
if (!string.IsNullOrEmpty(obj.LastName)) return obj.LastName;
return obj.FirstLastName.Split(... // your splitting logic goes here
}
var ids = objects.OrderBy(o => o.NameForSort()).Select(o => o.ObjectID).ToList();
When you really need this strange double solution then you will run into this and similar problems more often. As a more general solution, consider putting the business logic for names in a few read-only properties:
//for when the user's data is split in 2 fields
public string FirstName { get; set; }
public string LastName { get; set; }
//for when the user's data is all in one field
public string FirstLastName { get; set; }
public string FullName
{
get { ... } // pick from LastName, FirstName, FirstLastName
}
public string SortName
{
get { ... } // pick from LastName, FirstLastName
}
Once it determines the order of the sort (I can do that), how do I make it so that the output of the method is a list of ints representing ObjectID
result = MyObjectList
.OrderBy(m => m.SortName) // sort on SortName
.Select(m => m.ObjectID) // select the Id
.ToList();
If this sorting is specific to one use case, it can be achieved using LINQ:
var sortedIds = models.OrderBy(SecondName).Select(m => m.ObjectId).ToList();
private static string SecondName(MyObjectModel model)
{
if (!string.IsNullOrWhitespace(model.LastName)) return model.LastName;
return model.FirstLastName.Split(' ').Last();
}
While you can use LINQ, as others have suggested, that would involve creating a brand new list, not mutating the existing list. That may or may not be preferable. If you want to sort the list itself that's easy enough too:
List<string> list = new List<string>(){"a","b","c"};
list.Sort((a,b)=> a.CompareTo(b));
Just take your list, call Sort, and pass in a lambda that takes two items and returns an integer indicating which is greater. In your case, just call some method on a and b to get a string and then use CompareTo or string.Compare on those two strings.
I am developing an asp.net mvc application with NHibernate and I have a query where I would like to know how can I convert rows in columns?
I have a dynamic system with a model like this:
// It's a kind of metadata
public class Field
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
}
// the value is here with respective field
public class FieldValue
{
public virtual long Id { get; set; }
public virtual Field Field { get; set; }
public virtual string Value { get; set; }
}
I would like to know how can I get a result where columns are the Field objects and values are FieldValue objects, should I create a ViewModel? or a way to do it with asp.net mvc?
I can do a query to get a result like this:
But I would like to do a query like this (or a way to create a result on my View on the asp.net mvc):
Thanks
I think you are going to need to get fancy/familiar with GroupBy(). If you Group By the Field, you "group" all the values for a field then move to the next one. The pseudo-code would be something like the following
#model IEnumerable<FieldValue>
foreach(var fields in Model.GroupBy(x=>x.Field.Name){
<h2>fields.key</h2>
<ul>
foreach(var fieldValue in fields){
<li>#fieldValue.Value</li>
}
</ul>
}
This should render something like
Field 1
some value 1
some value 2
Field 2
some value 3
some value 4
etc.
By using this approach, you can control the formatting however you want to get the desired output.
Not clear with the question. if you want to transform rows into columns - you can use pivot. But i would suggest instead of operating on resultset to transpose, let it return the transposed result set. I meant handle the conversion of rows into columns in your query itself