开发者

fill linq null data c# from previous line

开发者 https://www.devze.com 2023-04-12 21:11 出处:网络
I have bill details in list which contains the heading: Accou开发者_开发百科nt Numbers, Dates, Service Number, Charge category, Details, exgst, ingst, Detailsfill

I have bill details in list which contains the heading:

Accou开发者_开发百科nt Numbers, Dates, Service Number, Charge category, Details, exgst, ingst, Detailsfill

Column :- Account Numbers, Dates, Service Number, Charge category, Details, exgst, ingst

are unique values but Column Detailsfill contains null. I want to allow my C# script to set values for Detailsfill where Detailsfill is null then insert data from previous row otherwise do nothing.

Note that Running total sets to 1 when detailsfill column has any text but continues to increment by 1 from 3 when detailsfill column is empty.

Below is the script I used.

var result_set2 = (from a in result_set1
                   join b in
   (from x in result_set1a select x)
on
   new {col1 = a.id, col2 = a.account, col3 = a.date, col4 = a.service, col5 = a.chargecat } 
   equals
   new { col1 = b.id, col2 = b.account, col3 = b.date, col4 = b.service, col5 = b.chargecat } into outer
      from tb in outer.DefaultIfEmpty()
      where a.running_total1 !=  0
        where a.running_total1 != 2
        where a.chargecat != ""
                   select new
                   {
   running_total = a.running_total1,
   account = a.account,
   date = a.date,
   service = a.service,
   chargecat = a.chargecat,
   details = a.details,
   exgst = a.exgst,
   ingst = a.ingst,
   detailsfill = ((tb == null) ? "" : tb.details)
 }).ToList();

 foreach (var line in result_set2)
{
    mobilenet_writerfile.WriteLine(line);

}


It sounds to me like you need a correlated subquery to populate detailsfill. This would be in the form of a select from a context with whatever parameters you determine fulfill your requirements. Example on second post of this thread: http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/18da37b0-b40e-444e-8ec7-7bd343e0c799/ Also you can find other examples here on stackoverflow: Is it possible to create a correlated subquery in LINQ?

More information would be required to write a contextual example but here is basically what it would look like:

Entities context = new Entities();
context.tbl.Where(t=>t.Id == someId)
    .Select(t=> new() {
        t.Id, 
        context.tbl2.First(tbl2=>tbl2.Id == t.Id).Value, //This is the subquery
        t.whatever});


This is not a normal operation of Linq to hold onto a previous value and then use it later. That's not to say you can't make that work with Linq, but it would be more straightforward to keep that logic outside of the sequence operation. Simply iterate over the results after the query is composed.

var item = results.First();
foreach (var obj in results)
{
    if (obj.detailsfill == null) 
        obj.detailsfill = item.detailsfill;

    item = obj;
}

Note: If the first item happens to contain null in detailsfill, you will not pick up a value until the first item in the sequence that does contain a value. To get the first such item up front, simply use

var item = results.First(o => o.detailsfill != null);
// loop below
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号