Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to know how to generate serial No using Entity Frame Work-
C#
public ActionResult AssistanceSchemeView()
        {
            List<cmsmobapp> obj = new List<cmsmobapp>();  
          int SNo=0;       
            var std = (from sa in Entities.MBL_MAST_ASST_SCHEME
                       join
                           saa in Entities.MBL_MAST_SchemeHead on (sa.Scheme_head_id) equals (saa.Scheme_Head_Id)
                       join la in Entities.MBL_MAST_LANGUAGE on sa.Lang_id equals la.Lang_id  
                       join ms in Entities.MBL_MAST_STATUS on sa.Status equals ms.Status
                        
                       select new
                       {
                          SNo++
                           sa.Scheme_Name,
                           sa.Scheme_id,
                           ms.StatusName,
                           sa
                           .Description,
                           sa.File_name,
                           saa.SchemeHead_Name,
                           sa.Lang_id,
                           la.LanguageName,
                           sa.Scheme_head_id,
                           sa.Status,
                           sa.Valid_from,
                           sa.Valid_to
                       }).ToList();            
            foreach (var p in std)
            {


                CMSMobApp edtprod = new CMSMobApp();

                edtprod.Scheme_Name = p.Scheme_Name;
                edtprod.Scheme_id = p.Scheme_id;
                edtprod.StatusName =  p.StatusName;
                edtprod.Lang_id = p.Lang_id;
                edtprod.Scheme_head_id = p.Scheme_head_id;
                edtprod.Status = p.Status;
                edtprod.Valid_from = p.Valid_from;
                edtprod.Valid_to = p.Valid_to;
                edtprod.Description = p.Description;
                edtprod.File_name = p.File_name;
                ViewBag.SchemeHead_Name = p.SchemeHead_Name;             
                obj.Add(edtprod);

            }
        

            return View("~/Views/Mob_App/AssistanceSchemeView.cshtml", obj.ToList());
        }


What I have tried:

increment S No using Entity Frame Work
Posted
Updated 6-Feb-18 9:14am
v2
Comments
David_Wimbley 6-Feb-18 11:45am    
Is all your trying to do increment SNo variable by 1? Just do it inside your foreach loop below your linq query.

1 solution

I'd suggest to check out this thread: c# - How to generate and auto increment Id with Entity Framework - Stack Overflow[^] and this: Auto Increment (Identity) does not work in Entity Framework | The ASP.NET Forums[^]

Conclusion: Autoincrement serial number handle by yourself is very poor idea for several reasons! This should be done on database (entity) level.

But, if you would like to know, how to create value which will be increasing inside a Linq query, check this:

C#
//lambda version
var result = Enumerable.Range(5,20)
	.Select((x,y) => new
	{
		index = y+1,
		value = x
	})
	.ToList();

//normal version
int index = 1;
var result2 = (from a in Enumerable.Range(5,20)
	select new
	{
		index = index++,
		value = a
	})
	.ToList();


Both produce the same result:
index value
1     5 
2     6 
3     7 
... 
20    24 
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900