Get invoice from Iqueryable <T> in linq-to-sql?

The following code doesn't seem to get the correct score .....

 var materials = consRepository.FindAllMaterials().AsQueryable();
 int count = materials.Count();

      

This is the way to do it ... Here is my repository that fetches the entries ...

public IQueryable<MaterialsObj> FindAllMaterials()
        {
           var materials =  from m in db.Materials
            join Mt in db.MeasurementTypes on m.MeasurementTypeId equals Mt.Id
                   where m.Is_Deleted == 0
                   select new MaterialsObj()
                   {
                       Id = Convert.ToInt64(m.Mat_id),
                       Mat_Name = m.Mat_Name,
                       Mes_Name = Mt.Name,
                   };
            return materials;

        }

      

Edit:

when i use this,

        var materials = consRepository.FindAllMaterials().AsQueryable();
        return View("Materials", materials);

      

I am getting 18 rows in my table ... So I cannot count the counter 18

, instead it gives12

Ans:

The breakpoint doesn't seem to give me the result, but the answer. Recorded (account) ...

+2


a source to share


2 answers


This should result in the correct score:

int count = consRepository.FindAllMaterials().Count();

      



How do you view the model in your view?

Is it possible that you are showing duplicate entries?

+2


a source


Is it possible that you are not joining the correct columns? The only reason I'm asking is because your id columns won't be consistent across every class. For Materials you use Mat_id

, but in MeasurementTypes you just use Id

. This makes me wonder if you are trying to concatenate the natural key value to an artificial primary key rather than the corresponding natural foreign key.



+1


a source







All Articles