实体框架 - 使用数据库中的列表填充多个对象,而无需预先处理每个对象

时间:2017-03-22 10:54:03

标签: c# entity-framework linq

我想从数据库填充列表。但是列表有多个对象。

的Class1

List<HotelRsRep> hotelRsReplst = new List<HotelRsRep>();

类对象

hotelRsReplst = db.HotelBoardCodesRep.Where(p=>p.description.Equals(des)).ToList();

试过

# BasicObject for minimal amount of methods
class Retarded < BasicObject
  def initialize(&value)
    @value = value
  end

  # Make sure we remove as many methods as we can to
  #  make the proxy more transparent.
  (instance_methods - %i(__id__ __send__ __binding__)).each do |name|
    undef_method name
  end

  def method_missing(*args, &block)
    # Get the actual value
    value = @value.call
    # Suppress warnings while we traverse constants
    warn_level, $VERBOSE = $VERBOSE, nil

    traversed_modules = []
    constant_finder = -> (root) do
      constant_name = root.constants.find do |constant|
        # We undefed ==, so we need a different way to compare.
        # Given that this class should be used for constant values in place,
        #  comparing object ids does the job.
        root.const_get(constant).__id__ == __id__
      end

      if constant_name
        # Just set the constant to the actual value
        root.const_set(constant_name, value)
      else
        # Recursively search for the containing module of the constant
        root.constants.each do |child_name|
          child_constant = root.const_get(child_name)
          if child_constant.is_a?(::Module) &&
               !traversed_modules.include?(child_constant)
            # Handle circular references
            traversed_modules.push child_constant
            constant_finder.(child_constant) 
          end
        end
      end
    end

    # ::Object is the root module where constants are contained
    constant_finder.(::Object)

    # Bring back warnings
    $VERBOSE = warn_level

    # We have already found the constant and set its value to whatever was
    #  passed in the constructor. However, this method_missing was called
    #  in an attempt to call a method on the value. We now should make that
    #  invocation.
    value.public_send(*args, &block)
  end
end

# I know, the example is mononic.
class EdwardKing
  DEAD = Retarded.new { [new('I'), new('II'), new('III')] }

  def initialize(number)
    @number = number
  end

  def whoami
    "Edward #{@number} of England"
  end
end

p EdwardKing::DEAD
  # [#<EdwardKing:0x007f90fc26fd10 @number="I">, #<EdwardKing:0x007f90fc26fc70 @number="II">, #<EdwardKing:0x007f90fc26fc20 @number="III">
p EdwardKing::DEAD.map(&:whoami)
  # ["Edward I of England", "Edward II of England", "Edward III of England"]

如何从db填充该hotelRsReplst对象?

3 个答案:

答案 0 :(得分:0)

我找到了解决方案。这样我就能填满我的目标。

var hotelRsReplst  = new HotelRsRep();
hotelRepList.boardCodes  = new List<HotelBoardCodesRep>();
hotelRepList.hotel = db.HotelBoardCodesRep.Where(p=>p.description.Equals(des)).ToList();

答案 1 :(得分:-1)

您可以使用Include。

详细说明: https://msdn.microsoft.com/en-us/library/bb738708(v=vs.110).aspx

答案 2 :(得分:-1)

我认为Linq最简单的方法就是使用include

hotelRsReplst = db.HotelBoardCodesRep.Include("boardCodes").Include("phones").Include("facilities").Where(p=>p.description.Equals(des)).ToList();