LINQ NOT Field LIKE String - String LIKE Field - linq

i want do to this:
SELECT table.field FROM table WHERE ((("030232334156") Like [field] & "*"));
(It's from Access, it works :))
with LINQ.
How can I do this?
It's NOT Field LIKE String, it's String LIKE Field.
Thanks for answers!

You should be able to use StartsWith
yourData.Where(m => "0302323-1".StartsWith(m.yourField));

Related

How to get table name for a simple Sequel Dataset object?

Ie, given a dataset object ds = DB[:transactions].where{updated_at > 1.day.ago} - no funny joins and stuff going on - how could I fetch the table name (:transactions) ?
If you want the first table in the dataset, you can use ds.first_source.
If you want it as a string you can do:
ds.first_source_table.to_s
If you want a symbol, just omit .to_s
Based on the example provided, I would do something like this.
ds.klass.name
That will return a string with the name of your table.

MongoDB C# LINQ serialization error

I have a class AttributeValue containing two string fields:
string DataType
string Category
My mongo query is as below:
var test19 = _repo.All().Where(p => p.Rule.Any(r => r.Target.AnyOf.Any(an => an.AllOf.Any(av => av.Match != null && policyFilters2.Contains(string.Concat(av.Match.AttributeValue.DataType, av.Match.AttributeValue.Category))))));
where policyFilters2 is List<string>
The above query gives me an error:
"Unable to determine the serialization information for the expression:
String.Concat(av.Match.AttributeValue.DataType,
av.Match.AttributeValue.Category)."
I am not sure what needs to be done to resolve this.
Any help is greatly appreciated.
I don't think MongoDB can do a search on concatenated values like that. Try concatenating the two fields separately in a field and then try the query.
Eventually, I had to search for both the fields using logical AND rather than concatenating

Finding Last Name Using LINQ

I am trying to get last name using linq in visual studio. In my database, i have the Field name like "FullName".
In this Field I have value like "Subbu Cargos"
I Wanna Display the "Cargos" in my textbox.
How can i make a simple linq query?
Would it be over simple to say:
return FullName.Split(' ').Last()
?
I would suggest breaking it up into different fields - Firstname, Middlename, lastname, Title - and rebuilding the name on the fly when you come to display it.
If you're still determined to use one field, then consider a query like:
string s = "Subba Cargos";
var lastnames = from name in s.Split(new Char[] { ' ' }).Last<string>()
select name;
I would suggest not trying to parse out the last name. Like you say, first and last names could be switched around, someone might have a second name, or a last name that consists of multiple words (“van Dijk”), or may not have entered a last name at all.
Check out this article: Falsehoods Programmers Believe About Names
If you still want to do this however, try something like this:
customers.Select(c => c.FullName.Split(' ').Last());
You might not be able to this on the server side. In that case:
customers
.Select(c => c.FullName)
.ToList()
.Select(n => n.Split(' ').Last());
Untested, but this should give a rough idea.
You could also do it like this:
customers
.Select (b => b.FullName.Substring ((b.FullName.IndexOf(' ') + 1)));

How do I query an integer column for "starts with" in Entity Framework?

I have a column that's defined as an integer in EF (Code First). I want to search it using "starts with." Now, I can do this:
Where(x => SqlFunctions.StringConvert((double)x.AccountNumber).StartsWith(searchTerm))
However, SqlFunctions.StringConvert() gets translated to the T-SQL function STR(), which left-pads the result for reasons which are beyond my comprehension.
Also, I can't use string.TrimStart() because it's not supported by the Entity Framework.
Can anyone lend any help?
Trim() and TrimStart() work in LINQ to Entities, so you can use:
Where(x => SqlFunctions.StringConvert((double)x.AccountNumber)
.TrimStart().StartsWith(searchTerm))
TrimStart translates into LTRIM in SQL. With searchTerm = 123 for example you get something like:
WHERE LTRIM(STR( CAST( [Extent1].[AccountNumber] AS float))) LIKE N'123%'

Convert SQL Query into LINQ Expression - LIKE operator on integer

Can someone convert the following simple SQL statement into LINQ? The StudentID is of type int.
select * from Student where studentId like '%1001%';
Thank you!
ctx.Student.Where(x => x.StudentId.Contains("1001"))
It's strange that you have an ID of string type.
Use int instead.
O, sorry, I see now, you wrote that ids are ints. In that case you cannot use like in SQL.
It doesnt make sense. You must convert int to string first.
ctx.Student.Where(x => x.StudentId.ToString().Contains("1001"))
Try this:
db.Students.Where(s => s.StudentID.ToString().Contains("1001"))
Use the Contains operator:
from s in Student
where s.studentId.ToString().Contains("1001")
select s;

Resources