Why this LINQ statement getting error on matching value?

Related searches

I have a list "otherfee" on which I am using a LINQ statement but it is showing error while I am matching o.ShortName

 IList <P_OtherFee> otherfee = 
     P_OtherFee.FindByP_ProgramId((int)p_LoanApplication.P_ProgramId);

        int fee = otherfee.Where(o => o.ShortName == 'PROCESSING FEE').Select(o => o);

You are assigning the whole IEnumerable<P_OtherFee> object to an int variable. You should filter the list and retrieve the property from a single object, but beware that it might be null, if nothing is found based on the conditions. Here is an example with the ternary operator to assign a default value of 0 in case obj is null:

P_OtherFee obj = otherFee.FirstOrDefault(o => o.ShortName == "PROCESSING FEE");
int fee = (obj != null) ? obj.Fee : 0; 

Old-school LINQ syntax might be simpler to read and understand:

int fee = (from obj in otherFee 
           where obj.ShortName == "PROCESSING FEE" 
           select obj.Fee).FirstOrDefault();

If a match is found, you get one Fee value, if there is no result, you get 0. This achieves the same result as the previous code, but saves you from writing a null check.

In both cases, you could use SingleOrDefault(...) instead of FirstOrDefault(...) if you expect exactly one result and want an exception to be raised if more than one result is obtained.

Sequence contains no matching element error in Linq Statement , var listOfWorkWeeks = daysInMonth .Select(day => new DateTime(year, month, day)) .GroupBy(d => cal.GetWeekOfYear(d, CalendarWeekRule. LINQ to JSON provides a number of methods for getting data from its objects. The index methods on JObject/JArray let you quickly get data by its property name on an object or index in a collection, while M:Newtonsoft.Json.Linq.JToken.

Because you are selecting the all object (o => o) and not an int property, you cannot set the result of your Where clause in your fee variable.

If you want to select one int :

int fee = otherfee.Select(o => o.intProperty).FirstOrDefault(o => o.ShortName == "PROCESSING FEE");

If you want to select several ints :

IEnumerable<int> fee = otherfee.Where(o => o.ShortName == "PROCESSING FEE").Select(o => o.intProperty);

And as @Sebastian Hofmann mentionned, you cannot use '' to declare a string, you have to use "".

[SOLVED], Although this compiles, I get the following error when I run: "Unable to create a constant value of type 'DemoProject.Models.ItemDatabase. I have 2 excel sheets , where one of them has one(dt1) column and the other has 2(dt2). I want to check if the value in dt1 matches the value in dt2 ( in the 2nd column) then get the value from it’s corresponding column ( which is (the 1st column). Here what I have so far: I have read both the excel file using read range and stored it to a variable data table I used for reach loop to get the

You can use Single()/SingleOrDefault() or First()/FirstOrDefault() to get fee value

var feeObj = otherfee.Single(o => o.ShortName == "PROCESSING FEE");  //If otherfee does not contain 'PROCESSING FEE' then it will throw an error
int fee = feeObj.ProcessingFee; 

Or

int? fee = otherfee.FirstOrDefault(o => o.ShortName == "PROCESSING FEE")?.FeeProcessing;

Or if you dont want use nullable data type of int then

int fee = otherfee.FirstOrDefault(o => o.ShortName == "PROCESSING FEE")?.FeeProcessing ?? 0;

You can read difference between Single() and First(): Here

In last code, I used ?. and ??, more more details read about navigation operator and null-coalescing operator in C#.

SanderSade/common-linq-mistakes, Contribute to SanderSade/common-linq-mistakes development by creating an Correct: I will get and use the first person named John Smith. If you have 10M rows, and your match is the very first row, Single() will still run and SingleOrDefault() won't – and return the default/null value, depending on the data type. Take� Common C# Programming Mistake #4: Using iterative (instead of declarative) statements to manipulate collections. In C# 3.0, the addition of Language-Integrated Query (LINQ) to the language changed forever the way collections are queried and manipulated. Since then, if you’re using iterative statements to manipulate collections, you didn’t use LINQ when you probably should have.

LINQ Count, LINQ Count method usage examples. Count in query expression. Commented LINQ query expression to count number of items which match specified predicate . If so, it simply returns value of its Count property. public static int Count< TSource>(this IEnumerable<TSource> source) { if (source == null) throw Error. The pattern matching is statement doesn't allow a nullable value type, such as int? or Nullable<int>, but you can test for any other value type. The is patterns from the preceding example are not limited to the nullable value types. You can also use those patterns to test if a variable of a reference type has a value or it's null.

LINQ Tutorial, If you write a LINQ query that can't be translated you will get a runtime exception and the error message will indicate which part of the query wasn't supported. With a predicate Any tests whether the collection has any matching documents. If the value of that field or property is represented in MongoDB as an array you� Here are a few reasons: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future. Makes code easier to read, which means it is less likely that errors will creep into it.

As you are fetching data using Linq from the database, so the type of the variable would be IQueryable<>, and you can materialize the reuslt in to a List<> by forcing the query to be executed and results to be brought in the memory by calling ToList() method.

Comments
  • It would help to provide which error..
  • .Select(o => o); LOL or do you mean, .FirstOrDefault(o => o.Id)
  • What is the expected input and output for this function?
  • I guess this is a typo: you should use "PROCESSING FEE" instead of 'PROCESSING FEE'
  • You are assigning an object List to an integer. And read the coment of @SebastianHofmann. Your used 'not "
  • I think this code would not work, because you can't implicitely convert IEnumerable<int> to int... And there's still the 'PROCESSING FEE' typo.
  • That's right, I answered too fast. I will edit my answer.
  • The first line of code doesn't compile, while the second one does, but it needs yet a few more lines to check the results and assign one to an int variable.
  • @PrasadTelkikar It would indeed be more efficient to Select before getting the results. I will update my answer.
  • The first two lines of code can be dangerous because you aren't handling any of the possible outcomes (SingleOrDefault is a safer option), while the other line of code doesn't compile (but I guess the "?" is a typo).
  • @StackLloyd, I already mentioned in comment about possible error while using single'. No ?. is not a typo just read about navigation operator in C#
  • I know about the operator, but this means you also need to declare fee as int?, which you didn't, and such a cast is unnecessary for such a simple operation.
  • You can simplify it with ??operator: int fee = otherfee.FirstOrDefault(o => o.ShortName == "PROCESSING FEE")?.FeeProcessing ?? 0;