I tried to compare two ids and get some result.it works for other strings.but not for this.
I tried like this.
var neededData = mainFaires.Where(c => c.trimacid == passId );
in here passId= OX20160330HAVHAV
and in the mainFaires list, in somewhere it includes this id.but it didn't give the result.I found in here
var x = mainFaires.ElementAt(27261);
this list include the same id.but didn't give result.I can't think why.
ElementAt is find the position.
You should use select to find the records
var x = mainFaires.Select(o => o.trimacid == 27261);
You should use .ToList() .First() or .FirstOrDefault() to actually commit the query and get a result. Your code only defined the query, but didn't actually submit it to the data collection.
If you expect only one item as a result, you're code should look like this:
var neededData = mainFaires.Where(c => c.trimacid == passId ).FirstOrDefault();
If there was no item found, neededData would be NULL or whatever the default value is. You may also check the Documentation here https://msdn.microsoft.com/en-us/library/system.linq.enumerable%28v=vs.100%29.aspx
Related
I have a small application that iterates over the results of a "Saved Search" retrieving the values from several Custom Columns(simplified example):
var results = searchResults.Select(a => new
{
X = ((SearchColumnBooleanCustomField)a.basic.customFieldList
.First(b => b.scriptId == "custentityX")).searchValue
Y = ((SearchColumnDateCustomField)a.basic.customFieldList
.First(b => b.scriptId == "custentityY")).searchValue
Z = ((SearchColumnSelectCustomField)a.basic.customFieldList
.First(b => b.scriptId == "custentityZ")).searchValue.name
}
For most returned column types I get a value consistent with the type(bool/date/etc...) but with the "SearchColumnSelectCustomField" I don't get any value in the returned "searchValue.name", it's always null, however the "searchValue.internalID" column is always populated. So for some reason its getting the selection but not returning the value from that selection.
How do I access the text value that I can see from the NetSuite interface from SuiteTalk("searchValue.name")? Do I have to execute another query to retrieve all value key pairs related to that internalID? For every custom field? And if so then what is the purpose of the name field in the first place?
I've tried searching around for this, but there's not really allot of documentation on the subject(or on SuiteTalk in general), in other languages(PHP/Java) people mention "getSelectValue"(here, and briefly here), I could try this in C#, but I'm not sure if these apply or if that can be done for custom value selections.
Then there's some references for determining the values BEFORE the search, this seems like overkill to me, is it really that hard? I have dozens of CustomFields I would like to access in my final application. Seems like there should be an easier way...
As far as I know, the web service response will only contain the internalId and typeId for a SearchColumnSelectCustomField. In order to get the name, you'll have to first query NetSuite to find all custom lists and their values.
You can do this using a CustomListSearch and set the bodyFieldsOnly search preference to false. Pass in no criteria to the CustomListSearch and you'll be returned every custom list and their values. Just store there results in memory and reference it when reading the column values from your saved search.
I tried the answer posted by #Adud123 and it works great, Here's what the code looks like:
public Dictionary<string, Dictionary<long, string>> getCustomFieldLists()
{
return
nsService.search(new CustomListSearch())
.recordList.Select(a => (CustomList) a)
.ToDictionary(a => a.internalId,
a => a.customValueList.customValue
.ToDictionary(b => b.valueId, c => c.value));
}
var valueLookup = getCustomFieldLists();
var results = searchResults.Select(a => new
{
Z = (a.basic.customFieldList.Where(b => b.scriptId == "custentityZ")
.Select(a => (SearchColumnSelectCustomField)a)
.Select(a => valueLookup[a.searchValue.typeId][a.searchValue.internalId])
.First()
}
Just some details. Get Records is a variable where it contains the results of my stored procedure. Now, what I want to ask is what if I want to remove the group by function but I still want to get the key and items? Is there a way to do it?
var sortResCinema = GetRecords.Where(x => test2.branch == x.Bbranch && test.movieName == x.MovieName && x.MovieName != null)
.GroupBy(x => x.MovieName,
(key, elements) =>
new
{
Id = key,
Items = elements.ToList()
}).ToList();
There's no need for GroupBy here since you are looking for a specific movieName.
I guess you wanted something like this:
var sortResCinema = GetRecords.Where(x => test2.branch == x.Bbranch && test.movieName == x.MovieName).ToList();
You can replace the GroupBy with a Select. The Select statement can be used to alter the type of the results returned, which is what you appear to want to do. Should work with exactly the same syntax as the second parameter. So replace "GroupBy" with "Select" and remove the first argument. The key and elements properties that are being used in the GroupBy statement are internal to that function so you'd need to work out what function you want to replace these by, for instance the key might be x.MovieName.
I need do a filter that request data with a parameter included in a list.
if (filter.Sc.Count > 0)
socios.Where(s => filter.Sc.Contains(s.ScID));
I try on this way but this not work, I tried also...
socios.Where( s => filter.Sc.All(f => f == s.ScID));
How I can do a filter like this?
socios.Where(s => filter.Sc.Contains(s.ScID));
returns a filtered query. It does not modify the query. You are ignoring the returned value. You need something like:
socios = socios.Where(s => filter.Sc.Contains(s.ScID));
but depending on the type of socios the exact syntax may be different.
In addition to needing to use the return value of your LINQ .Where(), you have a potential logic error in your second statement. The equivalent logic for a .Contains() is checking if Any of the elements pass the match criteria. In your case, the second statement would be
var filteredSocios = socios.Where( s => filter.Sc.Any(f => f == s.ScID));
Of course if you can compare object-to-object directly, the .Contains() is still adequate as long as you remember to use the return value.
I know this is simple, but my mind is playing tricks on me right now. If we have a flat list of objects with the properties GroupSortIndex and ItemSortIndex (within the group) and we want to find the first item in the list, what's the Linq/lambda for that?
About all I can think of is (meta, not literal code...)
var soughtItem = Source.OrderBy(ItemSortIndex).OrderBy(GroupSortIndex).ToList()[0]
...but that just looks so wrong to me for some reason.
Read post : Default Extension methods to get difference between first and firstordefault
you can use FirstOrDefualt() or First() function
var soughtItem = Source.OrderBy(ItemSortIndex).
ThenBy(GroupSortIndex).FirstOrDefualt();
if(soughtItem !=null)//advantage of using firstordefault
{
}
its better to use FirstOrDefualt because if there is no data it will return null intead of excetipn
You can use IOrderedEnumerable.ThenBy (Note: an IOrderedEnumerable is returned from IEnumerable.OrderBy):
var firstItem = source.OrderBy(s => s.GroupSortIndex)
.ThenBy(s => s.ItemSortIndex)
.First();
This orders first by the group and then by the item. You should use FirstOrDefault if the sequence can be empty. Otherwise First raises an exception.
(i've assumed that you want to order first by group and then by the item instead, since the ItemSortIndex is the index of the item within the group(as mentioned))
var soughtItem = Source
.OrderBy(ItemSortIndex)
.ThenBy(GroupSortIndex).First();
If ItemSortIndex and GroupSortIndex are properties instead of functions, then you need:
var soughtItem = Source
.OrderBy(i => ItemSortIndex)
.ThenBy(i => GroupSortIndex).First();
I have an array of ProgramIDs and would like to create a number of Select statements dynamically depending on how many ProgramIds there are.
For example:
var surveyProgramVar = surveyProgramRepository.Find().Where(x => x.ProgramId == resultsviewmodel.ProgramIds.FirstOrDefault());
This is an example of the select statement working with a single ProgramId.FirstOrDefault(). How do I create a list/array of SurveyProgramVars and select for each ProgramIds in the array?
It won't be necessarily optimal, but you might try:
var surveyProgramVar = surveyProgramRepository.Find()
.Where(x => resultsviewmodel.ProgramIds.Contains(x.ProgramId));
You could try something like:
var surveyProgramVar = surveyProgramRepository.Find().Where(x => resultsviewmodel.ProgramIds.Contains(x.ProgramId));
Tip: If the Find() method does a hit on a database, would be nice if you create a specific method to to a IN statment on the query. If you does not do this, it will take all records on a table and filter it in memory (linq to objects), which works but not very nice. Your code could be something like:
var surveyProgramVar = surveyProgramRepository.FindByProgramsId(resultsviewmodel.ProgramIds);