如何从此方法中获取列值?

时间:2013-11-09 14:11:00

标签: c# linq entity-framework ienumerable

我有一个方法,用于从我的表中获取名为ItemNumber,ItemDescription,UnitDescription的3列的值。我已经传递了所选行的参数ItemId,并且必须获取不同变量中的每个列值我不知道如何处理它。任何人都可以告诉我怎么做。这是我的代码 -

  public System.Collections.IEnumerable FetchDataItem(int id)
    {
        var row = (from t1 in context.InventoryUnit
                   join t2 in context.InventoryItem on t1.UnitId equals t2.UnitId
                   where t2.ItemId == id
                   orderby t2.ItemNumber, t2.ItemDescription
                   select new
                  {

                   t2.ItemNumber,
                   t2.ItemDescription,
                    t1.UnitDescription
                  });


        return row.ToList();
    }

1 个答案:

答案 0 :(得分:0)

Satpal提供了一个很好的例子,但是如果你不想创建一个类,你可以使用它并选择它作为匿名类型。

public static void Main()
{
    // Short example...
    var rows = (from t1 in context.InventoryUnit
                join t2 in context.InventoryItem on t1.UnitId equals t2.UnitId
                where t2.ItemId == id
                orderby t2.ItemNumber, t2.ItemDescription
                select new
                {
                    ItemNumber = t2.ItemNumber,
                    ItemDescription = t2.ItemDescription,
                    UnitDescription = t1.UnitDescription
                 });

    // Here you still have access to data and the types of the data.

    foreach (var row in row)
    {
        // do something with the data....
        var someint = row.ItemNumber;
        var someString = row.ItemDescription;
        var someotherstring = row.ItemDescription;
    }
}