如何使用LINQ从对象列表查询到现有对象?

时间:2010-11-15 16:02:47

标签: c# linq sharepoint

我不确定这在LINQ中是否可行,但我有以下情况:

我使用多个查询多次调用SharePoint列表服务。然后,我将从所有查询中填充单个对象及其属性。我正在使用LINQ来查询返回的XElement。我知道如果调用达到这一点,那么我的LINQ查询只返回一个项目。我目前必须查询一个新对象,然后为每个Web服务调用从这个新对象(来自LINQ)设置我的主对象的属性。 (以下代码示例仅包含需要查询和设置的“Action”项属性的一小部分。)

有没有办法让下面的语句'select'进入我现有的'action'对象?

var item = (from listItem in result.GetSPListItems()
            select new ContractAction
            {
                Title = listItem.GetSPFieldValue("Title"),
                Description = listItem.GetSPFieldValue("Description"),
                DeliveryOrderID = SPHelper.GetFirstLookupID(listItem.GetSPFieldValue("Delivery Order")),
                EstimatedValue = ((listItem.GetSPFieldValue("Estimated Value") as double?) ?? 0),
                AgreementTypeID = SPHelper.GetFirstLookupID(listItem.GetSPFieldValue("Contract Type")),                                                
            }).FirstOrDefault();

contractAction.Title = item.Title;
contractAction.Description = item.Description;
contractAction.DeliveryOrderID = item.DeliveryOrderID;
contractAction.EstimatedValue = item.EstimatedValue;
contractAction.AgreementTypeID = item.AgreementTypeID;

5 个答案:

答案 0 :(得分:1)

不能简单地说:

action = (from listItem in result.GetSPListItems()
         select new Action
         {
             Title = listItem.GetSPFieldValue("Title"),
             Description = listItem.GetSPFieldValue("Description"),
             DeliveryOrderID = SPHelper.GetFirstLookupID(listItem
                 .GetSPFieldValue("Delivery Order")),
             EstimatedValue = 
                 ((listItem.GetSPFieldValue("Estimated Value") as double?) ?? 0),
             AgreementTypeID = SPHelper.GetFirstLookupID(listItem
                 .GetSPFieldValue("Contract Type")),
         }).FirstOrDefault();

答案 1 :(得分:1)

首先,您应该重命名类Action,因为Action是内置库中已定义的委托。我将在以下代码中使用MyAction

如果您总是需要从MyAction获取SPListItem,您最好编写一个扩展方法,将逻辑放在一个地方进行DRY(不要重复自己)。

public static MyAction ToMyAction(this SPListItem item)
{
    return new MyAction  
       {  
          Title = item.GetSPFieldValue("Title"),  
          Description = item.GetSPFieldValue("Description"),  
          DeliveryOrderID = SPHelper.GetFirstLookupID(item.GetSPFieldValue("Delivery Order")),  
          EstimatedValue = ((item.GetSPFieldValue("Estimated Value") as double?) ?? 0),  
          AgreementTypeID = SPHelper.GetFirstLookupID(item.GetSPFieldValue("Contract Type"))                                                  
       };  
}

var action = result.GetSPListItems()
                   .Select(item => item.ToMyAction())
                   .FirstOrDefault();
//var action = (from item in result.GetSPListItems()
//              select item.ToMyAction()).FirstOrDefault();

答案 2 :(得分:1)

首先,因为我知道我只会得到一个结果,所以我不得不愿意删除使用linq查询的'select'部分的想法。一旦我这样做,答案显而易见。

//just grab the first item
var item = result.GetSPListItems().FirstOrDefault();

//then grab the properties into the existing ContractActionEntity                             
contractAction.Title = item.GetSPFieldValue("Title");
contractAction.Description = item.GetSPFieldValue("Description");
contractAction.DeliveryOrderID = SPHelper.GetFirstLookupID(item.GetSPFieldValue("Delivery Order"));
contractAction.EstimatedValue = item.GetSPFieldValue("Estimated Value").ToNullableDouble();
contractAction.AgreementTypeID = SPHelper.GetFirstLookupID(item.GetSPFieldValue("Contract Type")),

感谢大家让我更多地思考问题并引导我走向答案。

答案 3 :(得分:0)

您可以在一个大查询中编写查询。例如,您可以:

var item = (from listItem in result.GetSPListItems()
        from query2Outer in result.SecondQuery().Where(x => x.ItemEdp == it.ItemEdp).DefaultIfEmpty() // This is an outer join
        select new Action
        {
            //if the one query didn't return an item then it sets the properties to the default values
            Example = (query2Outer == null ? "Default Value" : query2Outer.Example),
            Title = listItem.GetSPFieldValue("Title"),
            Description = listItem.GetSPFieldValue("Description"),
            DeliveryOrderID = SPHelper.GetFirstLookupID(listItem.GetSPFieldValue("Delivery Order")),
            EstimatedValue = ((listItem.GetSPFieldValue("Estimated Value") as double?) ?? 0),
            AgreementTypeID = SPHelper.GetFirstLookupID(listItem.GetSPFieldValue("Contract Type")),                                                
        }).FirstOrDefault();

答案 4 :(得分:0)

扩展方法方法的替代方法是:

public static void SelectFirstInto(this IEnumerable<ContractAction> items, ContractAction target)
{
    var source = items.FirstOrDefault();

    if(source != null)
    {
        target.Title = source.Title,  
        target.Description = source.Description ,  
        target.DeliveryOrderID = source.DeliveryOrderID,  
        target.EstimatedValue = source.EstimatedValue, 
        target.AgreementTypeID = source.AgreementTypeID                                                 
     }
}

然后你可以这样做:

ContractAction targetAction = new ContractAction();
var item = (from listItem in result.GetSPListItems()
            select new ContractAction
            {
                Title = listItem.GetSPFieldValue("Title"),
                Description = listItem.GetSPFieldValue("Description"),
                DeliveryOrderID = SPHelper.GetFirstLookupID(listItem.GetSPFieldValue("Delivery Order")),
                EstimatedValue = ((listItem.GetSPFieldValue("Estimated Value") as double?) ?? 0),
                AgreementTypeID = SPHelper.GetFirstLookupID(listItem.GetSPFieldValue("Contract Type")),                                                
            }).SelectFirstInto(targetAction);

请记住,如果列表为空,则目标对象保持不变。这可能是你想要相应处理的事情。