Predicate matching a list [closed] - c#

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();

Related

I want compare two string and want to get possible matches [closed]

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
sb.Qualilfication_Master.First().Qual_List contain string values like "B.C.A,M.C.A,B.B.A"
and txtQualification.text contain string like "B.C.A, M.Com"
I want match above two thing
var sendnoti = (from p in db.Reg_JobSeeker_Masters where p.Qualification_Masters.First().Qual_List.Contains(txtQualification.Text)select p).ToList();
If I'm reading this right - and you want to find the strings that are common to two lists - then you can just use the intersect method.
I am just assuming txtQualification.Text is a List<string> so in that case you could just right it like this -
var sendnoti = (from p in db.Reg_JobSeeker_Masters where p.Qualification_Masters.First().Qual_List.Any(ql => txtQualification.Text.Contains(ql))select p).ToList();

Find item index with lambda or linkq in c# [closed]

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

String Contains Does Not Work In LINQ [closed]

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()

Argument Exception [closed]

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();

Find the object which contains an object with a specific property value in a nested list [closed]

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;
}

Categories