none
How to read specific field value when using LINQ to object to fetch data RRS feed

  • Question

  • see ny code

                        var data = dtFilterDataFromAllData.AsEnumerable().Where(x => x.Field<string>("Tab").ToString().Trim() == strBRTab
                            && x.Field<string>("Broker Items").ToString() == strBRLineItem
                            && x.Field<string>("Row").ToString().Trim() == RowNumber)
                            .Select(w => new { LinkText = w.Field<string>("LinkedItemList"), AllowBlank = w.Field<string>("Allow Blank in Calc.") }).ToList();
    where i am using LINQ on datatable to select data for two fields called LinkedItemList & Allow Blank in Calc.

    without using foreach how to read data from data variable like array like access ?

    how to read data like data["LinkedItemList"] 

    please tell me the way without foreach or for. thanks

    Wednesday, January 29, 2020 3:00 PM

Answers

  •                     var data = dtFilterDataFromAllData.AsEnumerable().Where(x => x.Field<string>("Tab").ToString().Trim() == strBRTab
                            && x.Field<string>("Broker Items").ToString() == strBRLineItem
                            && x.Field<string>("Row").ToString().Trim() == RowNumber)
                            .Select(w => new { LinkText = w.Field<string>("LinkedItemList"), AllowBlank = w.Field<string>("Allow Blank in Calc.") }).SingleOrDefault()

    If (data != null)

    {

       var help = data.sompropertyname;

    }

    The criteria seem to be looking for a single object, and you should be using SingleOrDefault() if that's the case.

    https://www.tutorialsteacher.com/linq/linq-element-operator-single-singleordefault

    • Marked as answer by Sudip_inn Thursday, January 30, 2020 4:17 PM
    Wednesday, January 29, 2020 8:02 PM

All replies

  •                     var data = dtFilterDataFromAllData.AsEnumerable().Where(x => x.Field<string>("Tab").ToString().Trim() == strBRTab
                            && x.Field<string>("Broker Items").ToString() == strBRLineItem
                            && x.Field<string>("Row").ToString().Trim() == RowNumber)
                            .Select(w => new { LinkText = w.Field<string>("LinkedItemList"), AllowBlank = w.Field<string>("Allow Blank in Calc.") }).SingleOrDefault()

    If (data != null)

    {

       var help = data.sompropertyname;

    }

    The criteria seem to be looking for a single object, and you should be using SingleOrDefault() if that's the case.

    https://www.tutorialsteacher.com/linq/linq-element-operator-single-singleordefault

    • Marked as answer by Sudip_inn Thursday, January 30, 2020 4:17 PM
    Wednesday, January 29, 2020 8:02 PM
  • see this code where i did not use any field name like LinkText etc

    var data = dtFilterDataFromAllData.AsEnumerable().Where(x => x.Field<string>("Tab").ToString().Trim() == strBRTab
                            && x.Field<string>("Broker Items").ToString() == strBRLineItem
                            && x.Field<string>("Row").ToString().Trim() == RowNumber)
                            .Select(w => new { w.Field<string>("LinkedItemList"), w.Field<string>("Allow Blank in Calc.") }).ToList();

    in this case how could i print the value of link text without foreach. i need to access linktext like array ordinal position. please help

    Thursday, January 30, 2020 4:17 PM
  • in this case how could i print the value of link text without foreach. i need to access linktext like array ordinal position. please help

    Linq returns the result of a Linq query in a tubular form, a collection of objects, and therefore, one must use a loop to access objects in the collection. The only time a collection is not used is when Single() or First() is used. 

    Thursday, January 30, 2020 4:40 PM
  • suppose first or single used instead of Tolist() then how could i access fields both LinkedItemList & Allow Blank in Calc like ordinal position like array instead of foreach loop.

    please give a sample code. thanks

    Friday, January 31, 2020 12:49 PM