小编Gre*_*opp的帖子

如何在Entity Framework中动态构造Where Expression?

我已经看过这个关于如何在Entity Framework中动态创建OrderBy表达式的答案.但我还想构建一个动态Where表达式.有点像这样:

public IEnumerable<InventoryItem> GetAll(string filterBy, object value)
{
  var results = new List<InventoryItem>();
  using (var db = new InventoryDb())
  {
    if (QueryHelper.PropertyExists<InventoryItem>(filterBy)) 
    {
      var query = db.rri_InventoryItems.WhereByProperty(filterBy, value); 

      foreach(var item in query.Where(expr))
      {
        results.Add(ConvertItem(item));
      }   
    }            
  }
  return results;
}
Run Code Online (Sandbox Code Playgroud)

传入属性以过滤by和值作为ab对象.Queryable有两种方法,哪里都有两个参数,所以我甚至不确定哪一个是正确的.

而且在这一点上我失去了一点点.我不确定如何重构原始的OrderByProerty方法来提供WhereByProperty.我知道我在这里完全错了.我不知道该怎么做.

理想情况下,我想通过提供可用于使用andsor运算符构建查询的对象集合来进一步扩展它.

linq entity-framework expression-trees where-clause

7
推荐指数
1
解决办法
2528
查看次数

xsd.exe使用数组中的多个元素生成c#

我有一组XML模式文件提供给我.我无法更改XML,因为这些有时会更新.我正在使用xsd.exe将架构文件转换为生成的c#代码.我不能使用任何第三方工具.其中一个XML模式文件的一部分显示如下:

<xs:complexType name="LocationType">
  <xs:choice minOccurs="1" maxOccurs="1">
    <xs:element name="LocNum" minOccurs="1" maxOccurs="1" type="xs:string" />
    <xs:sequence>
      <xs:element name="Name" minOccurs="0" maxOccurs="1" type="xs:string" />
      <xs:element name="Address" minOccurs="0" maxOccurs="1" type="xs:string" />
      <xs:element name="City" minOccurs="1" maxOccurs="1" type="xs:string" />
      <xs:element name="State" minOccurs="0" maxOccurs="1">
    </xs:sequence>
  </xs:choice>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)

当转换为c#时,我得到如下结果:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://abcxyz.com")]
public partial class LocationType
{

    private object[] itemsField;

    private ItemsChoiceType[] itemsElementNameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Address", typeof(string), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("City", typeof(string), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("LocNum", typeof(string), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("Longitude", typeof(decimal), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] …
Run Code Online (Sandbox Code Playgroud)

c# xml xsd

6
推荐指数
2
解决办法
3578
查看次数

标签 统计

c# ×1

entity-framework ×1

expression-trees ×1

linq ×1

where-clause ×1

xml ×1

xsd ×1