Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So I have an application that uses the Newtonsoft.Json dll and I need to search a JSON file for a value entered by the user, then select the node with that value in.
JSON Snippet
So the user will search for the 'name' tag and I need to be able to fetch the 'ID' of the node.
Example:
Name: Blimp
-----------------Fetching ID-----------------
ID: 302686
Seeing as the item you want to (actually) search is an array, you'd have to deserialize the JSON into a C# object. Then, you can access the array, on which you can unleash search functions like a LINQ expression:
var item = Array
.Where(x => x.name == given_name)
.Select(x => x.id);
or Lambda:
var index = Array.FindIndex(x => x.name.Equals(given_name));
var item = Array[index];
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Hi i can't speak english well forgive me if ill confuse you.
in c# i have 3 String List.
list_one: list of file address.
list_two: list of MD5 that makes with list_one.
list_three: list of MD5 that makes with list_two but in this list i collect duplicate item from list_two
Question :
How can i get each item in list_three and search that in list_two then return that index.
but i dont like to use for or foreach because that will slow my application.
how can do that with linq or lambda or any fastest way.
my lists Image
No 1 foeach isn't slower. But to answer what you want is simple one liner like this.
using System.Linq;
List<string> list = new List<string>{"a","b","c","d"};
List<string> list2 = new List<string>{"a","c"};
var result = list.Select((a, b) => new {Value = a, Index = b})
.Where(x => list2.Any(d => d == x.Value))
.Select(c => c.Index).ToArray();
now result contains all the match indexes.Fiddle
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to do something like -
List<int> accountList = new List<int>();
accountList .Add(1);
accountList .Add(27);
var rec = _db.Accounts.Where(a=> accountList.Contains(a.accountId)).Take(10);
My code is a little more complicated than this - there are several other conditions in the where clause, but this is the bit that is causing problems - nothing gets returned even when there are matching values.
Basically I want it to retrieve all the records where accountId matches a value in my list.
Any pointers?
The sample above is giving me a cant convert lambda error.
You are missing a bit on your contains version
var hold2 = _db.Accounts.Where(a => find.Contains(a.accountDd)).Take(10).ToList();
Have you tried using any
var hold2 = _db.Accounts.Where(a => accountList.Any(m => m == a.accountId)).Take(10).ToList();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I've always used Contains method to gain SQL IN functionality to my LINQ queries.
But this time, very strange thing occured.
I've got an array of string like this :
string[] FilenamesToParse = {"JOHN_X200-", "DOE_X300-", "FOO_X300_M-"};
Then I've used this array just like below:
var result = (from dps in appProcessList
where FilenamesToParse.Contains(dps.FileName)
select dps.Devices).ToList()
Above query resulted with 0 result but I'm sure that there are filenames contains words defined in FilenamesToParse array.
So I've tried below snippet and Contains worked.
foreach (var applicationProcess in appProcessList)
{
if (applicationProcess.FileName.Contains(FilenamesToParse[0]))
{
}
}
Where am I wrong here ?
Thanks in advance.
Your two appraoches aren´t similar. In your linq you´re iterating your FilenamesToParse-array and check if any of its elements exactly matches dps.FileName, whereby in the second one you iterate appProcessList and check if its FileName-property contains the first FilenamesToParse.
The following would be the linq-approach similar to your loop:
var result = (from dps in appProcessList
where FilenamesToParse.Any(x => dps.FileName.Contains(x))
select dps.Devices).ToList()
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Building a list from model in C# using Entity Framework. I am using LINQ to try to match up the first 6 or 7 characters in a field. In a language other than C# I would use a regex expression.. or in SQL a "Like" with wildcard symbols. Below is the code I'm using, and I am getting an Argument exception.
List<InvoiceHeader> tempData = db.InvoiceHeader
.Where(f => f.ivh_invoicenumber.Any(t => f.ivh_invoicenumber.StartsWith(temp))).ToList();
If ivh_invoicenumber and temp are of type string
var tempData = db.InvoiceHeader.Where(f => f.ivh_invoicenumber.StartsWith(temp)).ToList();
If temp is array of strings:
var tempData = db.InvoiceHeader
.Where(f => temp.Any(t => f.ivh_invoicenumber.StartsWith(t))).ToList();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a generated c# class from a XML file and I need to search in a Class Object. I will find a specified string in a List have a List but I don't know how to do this, I have tryed this but It wont work:
AdrType = HeaderObj.ClientObj.OrgObj.Addresses.First( s => s.AddressTypes.Select( aD => aD._Type_.Where(sX => sX = "a"))).AddressCode
Something like this?
var adressWithTypeA = Addresses
.FirstOrDefault( s => s.AddressTypes.Any(x => x._Type_ == "a"))
if (adressWithTypeA != null)
{
var adressCode = adressWithTypeA.AddressCode;
}